Skip to content

Commit

Permalink
apply standard:fix
Browse files Browse the repository at this point in the history
Mostly it’s `Performance/StringIdentifierArgument`: methods like `instance_variable_get` or `send` should use symbols, not strings.

However, `const_get` does not accepts symbols with nested constants, like `Smth::Setting` (see this issue: rubocop/rubocop-performance#425, which might get fixed by rubocop/rubocop-performance#427). I’ve added `# standard:disable` in `spec/property_sets_spec.rb` until that fix is released.
  • Loading branch information
razumau committed Jan 3, 2024
1 parent 1d4096e commit f9554be
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/property_sets/action_view_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(property_set, template, object_name, object)

def check_box(property, options = {}, checked_value = "1", unchecked_value = "0")
options = prepare_options(property, options) do |properties|
properties.send("#{property}?")
properties.send(:"#{property}?")
end
template.check_box(object_name, property, options, checked_value, unchecked_value)
end
Expand Down Expand Up @@ -59,7 +59,7 @@ def prepare_id_name(property, options)
end

def fetch_target_object
instance = template.instance_variable_get("@#{object_name}")
instance = template.instance_variable_get(:"@#{object_name}")

throw "No @#{object_name} in scope" if instance.nil?
throw "The property_set_check_box only works on models with property set #{property_set}" unless instance.respond_to?(property_set)
Expand Down
20 changes: 10 additions & 10 deletions lib/property_sets/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def property_set(association, options = {}, &block)
raise "Invalid property key #{key}" if respond_to?(key)

# Reports the coerced truth value of the property
define_method "#{key}?" do
define_method :"#{key}?" do
type = property_class.type(key)
value = lookup_value(type, key)
!["false", "0", "", "off", "n"].member?(value.to_s.downcase)
Expand All @@ -72,13 +72,13 @@ def property_set(association, options = {}, &block)
end

# Assigns a new value to the property
define_method "#{key}=" do |value|
define_method :"#{key}=" do |value|
instance = lookup(key)
instance.value = PropertySets::Casting.write(property_class.type(key), value)
instance.value
end

define_method "#{key}_record" do
define_method :"#{key}_record" do
lookup(key)
end
end
Expand Down Expand Up @@ -113,7 +113,7 @@ def set(property_pairs, with_protection = false)
if with_protection && record.protected?
association_class.logger.warn("Someone tried to update the protected #{name} property to #{property_pairs[name]}")
else
send("#{name}=", property_pairs[name])
send(:"#{name}=", property_pairs[name])
end
end
end
Expand All @@ -131,11 +131,11 @@ def protected?(arg)
end

def enable(arg)
send("#{arg}=", "1")
send(:"#{arg}=", "1")
end

def disable(arg)
send("#{arg}=", "0")
send(:"#{arg}=", "0")
end

def build_default(arg)
Expand Down Expand Up @@ -170,7 +170,7 @@ def lookup(arg)

owner = proxy_association.owner

instance.send("#{association_class.owner_class_sym}=", owner) if owner.new_record?
instance.send(:"#{association_class.owner_class_sym}=", owner) if owner.new_record?
instance
end

Expand Down Expand Up @@ -216,7 +216,7 @@ def update_columns(attributes)
attributes = attributes.reject { |k, _| self.class.delegated_property_set_attributes.include?(k.to_s) }
end

super attributes
super(attributes)
end

private
Expand All @@ -226,11 +226,11 @@ def delegated_property_sets?
end

def attributes_for_create(attribute_names)
super filter_delegated_property_set_attributes(attribute_names)
super(filter_delegated_property_set_attributes(attribute_names))
end

def attributes_for_update(attribute_names)
super filter_delegated_property_set_attributes(attribute_names)
super(filter_delegated_property_set_attributes(attribute_names))
end

def filter_delegated_property_set_attributes(attribute_names)
Expand Down
14 changes: 7 additions & 7 deletions lib/property_sets/delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ def delegate_to_property_set(setname, mappings)
type = association.association_class.type(new_attr)
association.lookup_value(type, new_attr)
}
alias_method "#{old_attr}_before_type_cast", old_attr
define_method("#{old_attr}?") { send(setname).send("#{new_attr}?") }
define_method("#{old_attr}=") do |value|
alias_method :"#{old_attr}_before_type_cast", old_attr
define_method(:"#{old_attr}?") { send(setname).send(:"#{new_attr}?") }
define_method(:"#{old_attr}=") do |value|
if send(old_attr) != value
send("#{old_attr}_will_change!")
send(:"#{old_attr}_will_change!")
end
send(setname).send("#{new_attr}=", value)
send(setname).send(:"#{new_attr}=", value)
super(value)
end

define_method("#{old_attr}_will_change!") do
define_method(:"#{old_attr}_will_change!") do
attribute_will_change!(old_attr)
end

define_method("#{old_attr}_changed?") do
define_method(:"#{old_attr}_changed?") do
collection_proxy = send(setname)
return false unless collection_proxy.loaded?
setting = collection_proxy.lookup_without_default(new_attr)
Expand Down
2 changes: 1 addition & 1 deletion lib/property_sets/property_set_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def owner_class_sym
end

def owner_class_key_sym
"#{owner_class_sym}_id".to_sym
:"#{owner_class_sym}_id"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/property_sets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class AnotherThing < MainDatabase # standard:disable Lint/ConstantDefinitionInBl
# Because we are running these specs with two separate classes
# (Parent::Account & Parent::AccountAltDb), we need to build the
# settings class class name manually.
settings_klass = Object.const_get("#{account_klass}Setting")
settings_klass = Object.const_get("#{account_klass}Setting") # standard:disable Performance/StringIdentifierArgument
s = settings_klass.new(account.model_name.element.to_sym => account)

valids = %w[hello hel_lo hell0] + [:hello]
Expand Down
6 changes: 3 additions & 3 deletions spec/view_extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def base_options
allow(object).to receive(property_set).and_return(double("Fake property", property => "value"))
allow(template).to receive(:hidden_field)

expect(template).to receive(:instance_variable_get).with("@#{object_name}").and_return(object)
expect(template).to receive(:instance_variable_get).with(:"@#{object_name}").and_return(object)
proxy.hidden_field(property)
end
end

describe "#check_box" do
describe "when called with checked true for a truth value" do
before do
settings = double("Fake setting", property => "1", "#{property}?".to_sym => true)
settings = double("Fake setting", property => "1", :"#{property}?" => true)
allow(object).to receive(property_set).and_return(settings)
end

Expand All @@ -50,7 +50,7 @@ def base_options

describe "when called with checked false for a truth value" do
before do
settings = double("Fake setting", property => "0", "#{property}?".to_sym => false)
settings = double("Fake setting", property => "0", :"#{property}?" => false)
allow(object).to receive(property_set).and_return(settings)
end

Expand Down

0 comments on commit f9554be

Please sign in to comment.