Skip to content

Commit

Permalink
chore: update formatting and style of all files (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros authored Dec 5, 2021
1 parent 2a8acc1 commit e0f859e
Show file tree
Hide file tree
Showing 27 changed files with 1,101 additions and 1,001 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
strategy:
matrix:
ruby:
- 2.5
- 2.6
- 2.7
- 3.0
- 2.7
- 2.6
- 2.5
- head
gemfile:
- gemfiles/activerecord_6.0.gemfile
Expand All @@ -29,6 +29,9 @@ jobs:
- ruby: truffleruby-head
db: postgresql
gemfile: gemfiles/activerecord_6.1.gemfile
- ruby: truffleruby-head
db: postgresql
gemfile: gemfiles/activerecord_7.0.gemfile
exclude:
- ruby: 2.5
gemfile: gemfiles/activerecord_7.0.gemfile
Expand Down
14 changes: 10 additions & 4 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@

# frozen_string_literal: true

appraise 'activerecord-6.0' do
gem 'activerecord', "~> 6.0.0"
gem 'activerecord', '~> 6.0.0'
gem 'pg'
gem 'mysql2', '~> 0.5'
end

appraise 'activerecord-6.1' do
gem 'activerecord', "~> 6.1.0"
gem 'activerecord', '~> 6.1.0'
gem 'pg'
gem 'mysql2', '~> 0.5'
end

appraise 'activerecord-7.0' do
gem 'activerecord', "~> 7.0.0.alpha2"
gem 'activerecord', '~> 7.0.0.alpha2'
gem 'pg'
gem 'mysql2', '~> 0.5'
end
10 changes: 2 additions & 8 deletions gemfiles/activerecord_6.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
source "https://rubygems.org"

gem "activerecord", "~> 6.0.0"
case ENV["DB"]
when "postgresql"
gem 'pg'
when "mysql"
gem 'mysql2', '~> 0.5'
else
gem 'sqlite3'
end
gem "pg"
gem "mysql2", "~> 0.5"

group :local_development do
gem "guard"
Expand Down
11 changes: 3 additions & 8 deletions gemfiles/activerecord_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
source "https://rubygems.org"

gem "activerecord", "~> 6.1.0"
case ENV["DB"]
when "postgresql"
gem 'pg'
when "mysql"
gem 'mysql2', '~> 0.5'
else
gem 'sqlite3'
end
gem "pg"
gem "mysql2", "~> 0.5"

group :local_development do
gem "guard"
gem "guard-rspec"
Expand Down
11 changes: 3 additions & 8 deletions gemfiles/activerecord_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
source "https://rubygems.org"

gem "activerecord", "~> 7.0.0.alpha2"
case ENV["DB"]
when "postgresql"
gem 'pg'
when "mysql"
gem 'mysql2', '~> 0.5'
else
gem 'sqlite3'
end
gem "pg"
gem "mysql2", "~> 0.5"

group :local_development do
gem "guard"
gem "guard-rspec"
Expand Down
18 changes: 8 additions & 10 deletions lib/acts_as_taggable_on/default_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ActsAsTaggableOn
##
# Returns a new TagList using the given tag string.
Expand All @@ -6,41 +8,39 @@ module ActsAsTaggableOn
# tag_list = ActsAsTaggableOn::DefaultParser.parse("One , Two, Three")
# tag_list # ["One", "Two", "Three"]
class DefaultParser < GenericParser

def parse
string = @tag_list

string = string.join(ActsAsTaggableOn.glue) if string.respond_to?(:join)
TagList.new.tap do |tag_list|
string = string.to_s.dup

string.gsub!(double_quote_pattern) {
string.gsub!(double_quote_pattern) do
# Append the matched tag to the tag list
tag_list << Regexp.last_match[2]
# Return the matched delimiter ($3) to replace the matched items
''
}
end

string.gsub!(single_quote_pattern) {
string.gsub!(single_quote_pattern) do
# Append the matched tag ($2) to the tag list
tag_list << Regexp.last_match[2]
# Return an empty string to replace the matched items
''
}
end

# split the string by the delimiter
# and add to the tag_list
tag_list.add(string.split(Regexp.new delimiter))
tag_list.add(string.split(Regexp.new(delimiter)))
end
end


# private
def delimiter
# Parse the quoted tags
d = ActsAsTaggableOn.delimiter
# Separate multiple delimiters by bitwise operator
d = d.join('|') if d.kind_of?(Array)
d = d.join('|') if d.is_a?(Array)
d
end

Expand Down Expand Up @@ -73,7 +73,5 @@ def double_quote_pattern
def single_quote_pattern
/(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/
end

end

end
2 changes: 2 additions & 0 deletions lib/acts_as_taggable_on/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ActsAsTaggableOn
class Engine < Rails::Engine
end
Expand Down
2 changes: 2 additions & 0 deletions lib/acts_as_taggable_on/generic_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ActsAsTaggableOn
##
# Returns a new TagList using the given tag string.
Expand Down
60 changes: 30 additions & 30 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8
# frozen_string_literal: true

module ActsAsTaggableOn
class Tag < ::ActiveRecord::Base
self.table_name = ActsAsTaggableOn.tags_table
Expand Down Expand Up @@ -31,41 +32,43 @@ def self.named(name)
end

def self.named_any(list)
clause = list.map { |tag|
clause = list.map do |tag|
sanitize_sql_for_named_any(tag).force_encoding('BINARY')
}.join(' OR ')
end.join(' OR ')
where(clause)
end

def self.named_like(name)
clause = ["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'", "%#{ActsAsTaggableOn::Utils.escape_like(name)}%"]
clause = ["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'",
"%#{ActsAsTaggableOn::Utils.escape_like(name)}%"]
where(clause)
end

def self.named_like_any(list)
clause = list.map { |tag|
sanitize_sql(["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'", "%#{ActsAsTaggableOn::Utils.escape_like(tag.to_s)}%"])
}.join(' OR ')
clause = list.map do |tag|
sanitize_sql(["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'",
"%#{ActsAsTaggableOn::Utils.escape_like(tag.to_s)}%"])
end.join(' OR ')
where(clause)
end

def self.for_context(context)
joins(:taggings).
where(["#{ActsAsTaggableOn.taggings_table}.context = ?", context]).
select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
joins(:taggings)
.where(["#{ActsAsTaggableOn.taggings_table}.context = ?", context])
.select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end

def self.for_tenant(tenant)
joins(:taggings).
where("#{ActsAsTaggableOn.taggings_table}.tenant = ?", tenant.to_s).
select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
joins(:taggings)
.where("#{ActsAsTaggableOn.taggings_table}.tenant = ?", tenant.to_s)
.select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end

### CLASS METHODS:

def self.find_or_create_with_like_by_name(name)
if ActsAsTaggableOn.strict_case_match
self.find_or_create_all_with_like_by_name([name]).first
find_or_create_all_with_like_by_name([name]).first
else
named_like(name).first || create(name: name)
end
Expand All @@ -78,27 +81,25 @@ def self.find_or_create_all_with_like_by_name(*list)

existing_tags = named_any(list)
list.map do |tag_name|
begin
tries ||= 3
comparable_tag_name = comparable_name(tag_name)
existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
existing_tag || create(name: tag_name)
rescue ActiveRecord::RecordNotUnique
if (tries -= 1).positive?
ActiveRecord::Base.connection.execute 'ROLLBACK'
existing_tags = named_any(list)
retry
end

raise DuplicateTagError.new("'#{tag_name}' has already been taken")
tries ||= 3
comparable_tag_name = comparable_name(tag_name)
existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
existing_tag || create(name: tag_name)
rescue ActiveRecord::RecordNotUnique
if (tries -= 1).positive?
ActiveRecord::Base.connection.execute 'ROLLBACK'
existing_tags = named_any(list)
retry
end

raise DuplicateTagError, "'#{tag_name}' has already been taken"
end
end

### INSTANCE METHODS:

def ==(object)
super || (object.is_a?(Tag) && name == object.name)
def ==(other)
super || (other.is_a?(Tag) && name == other.name)
end

def to_s
Expand All @@ -110,7 +111,6 @@ def count
end

class << self

private

def comparable_name(str)
Expand Down
19 changes: 8 additions & 11 deletions lib/acts_as_taggable_on/tag_list.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require 'active_support/core_ext/module/delegation'

module ActsAsTaggableOn
class TagList < Array
attr_accessor :owner
attr_accessor :parser
attr_accessor :owner, :parser

def initialize(*args)
@parser = ActsAsTaggableOn.default_parser
Expand Down Expand Up @@ -34,8 +34,8 @@ def <<(obj)

# Concatenation --- Returns a new tag list built by concatenating the
# two tag lists together to produce a third tag list.
def +(other_tag_list)
TagList.new.add(self).add(other_tag_list)
def +(other)
TagList.new.add(self).add(other)
end

# Appends the elements of +other_tag_list+ to +self+.
Expand Down Expand Up @@ -65,12 +65,12 @@ def remove(*names)
# tag_list = TagList.new("Round", "Square,Cube")
# tag_list.to_s # 'Round, "Square,Cube"'
def to_s
tags = frozen? ? self.dup : self
tags = frozen? ? dup : self
tags.send(:clean!)

tags.map do |name|
d = ActsAsTaggableOn.delimiter
d = Regexp.new d.join('|') if d.kind_of? Array
d = Regexp.new d.join('|') if d.is_a? Array
name.index(d) ? "\"#{name}\"" : name
end.join(ActsAsTaggableOn.glue)
end
Expand All @@ -85,22 +85,19 @@ def clean!
map! { |tag| tag.mb_chars.downcase.to_s } if ActsAsTaggableOn.force_lowercase
map!(&:parameterize) if ActsAsTaggableOn.force_parameterize

ActsAsTaggableOn.strict_case_match ? uniq! : uniq!{ |tag| tag.downcase }
ActsAsTaggableOn.strict_case_match ? uniq! : uniq!(&:downcase)
self
end


def extract_and_apply_options!(args)
options = args.last.is_a?(Hash) ? args.pop : {}
options.assert_valid_keys :parse, :parser

parser = options[:parser] ? options[:parser] : @parser
parser = options[:parser] || @parser

args.map! { |a| parser.new(a).parse } if options[:parse] || options[:parser]

args.flatten!
end

end
end

Loading

0 comments on commit e0f859e

Please sign in to comment.