diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..d5b0470 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,3 @@ +0f442b1777212eb5f0574f1d66d8692d7b42c94d +9f9fbc7e8e711fd3c3f1377c415dffba14d08d48 +fa5717a68557d679b76f8a485466bd3192c06ad7 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fdad17..88f9f3e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,11 @@ name: CI -on: [pull_request, push, workflow_dispatch] +on: + pull_request: + push: + branches: + - master + workflow_dispatch: jobs: specs: @@ -29,7 +34,7 @@ jobs: BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile LEGACY_CONNECTION_HANDLING: ${{ matrix.legacy_connection_handling }} steps: - - uses: zendesk/checkout@v2 + - uses: zendesk/checkout@v4 - name: Set up Ruby uses: zendesk/setup-ruby@v1 with: @@ -52,3 +57,14 @@ jobs: echo "Some specs failed" false fi + + lint: + runs-on: ubuntu-latest + steps: + - uses: zendesk/checkout@v4 + - name: Set up Ruby + uses: zendesk/setup-ruby@v1 + with: + ruby-version: "2.7" + bundler-cache: true + - run: bundle exec rake standard diff --git a/Rakefile b/Rakefile index 9287292..15c219a 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,9 @@ -require 'bundler/gem_tasks' -require 'bump/tasks' -require 'rspec/core/rake_task' +require "bundler/gem_tasks" +require "bump/tasks" +require "rspec/core/rake_task" +require "standard/rake" -task default: :spec +task default: [:spec, :standard] # Pushing to rubygems is handled by a github workflow ENV["gem_push"] = "false" diff --git a/benchmark/read.rb b/benchmark/read.rb index f60ea43..3eb3bcb 100644 --- a/benchmark/read.rb +++ b/benchmark/read.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/../test/helper') +require File.expand_path(File.dirname(__FILE__) + "/../test/helper") class Account < ActiveRecord::Base # Benchmark reading from an object with many settings when: @@ -9,31 +9,28 @@ class Account < ActiveRecord::Base property_set :benchmark_settings do # 30 simple objects 10.times do |i| - property "float_prop_#{i}", :type => :float, :default => 3.1415 - property "int_prop_#{i}", :type => :integer, :default => 22 - property "string_prop_#{i}", :type => :string, :default => "Sausalito, CA" + property "float_prop_#{i}", type: :float, default: 3.1415 + property "int_prop_#{i}", type: :integer, default: 22 + property "string_prop_#{i}", type: :string, default: "Sausalito, CA" end # 10 complex 5.times do |i| - property "datetime_prop_#{i}", :type => :datetime, :default => Time.now.to_s - property "serialized_prop_#{i}", :type => :serialized, :default => { "Hello" => "There" } + property "datetime_prop_#{i}", type: :datetime, default: Time.now.to_s + property "serialized_prop_#{i}", type: :serialized, default: {"Hello" => "There"} end # 60 booleans 60.times do |i| - property "boolean_prop_#{i}", :type => :boolean, :default => true + property "boolean_prop_#{i}", type: :boolean, default: true end end - end class BenchmarkRead < ActiveSupport::TestCase - context "property sets" do - setup do - @account = Account.create(:name => "Name") + @account = Account.create(name: "Name") end should "benchmark fully defined settings" do @@ -65,7 +62,6 @@ class BenchmarkRead < ActiveSupport::TestCase end puts "Reading empty settings: #{empty_timing}ms" end - end def read_settings(account) @@ -78,5 +74,4 @@ def read_settings(account) account.benchmark_settings.boolean_prop_40? account.benchmark_settings.boolean_prop_50? end - end diff --git a/gemfiles/rails6.0.gemfile b/gemfiles/rails6.0.gemfile index 5d3099d..30a0b42 100644 --- a/gemfiles/rails6.0.gemfile +++ b/gemfiles/rails6.0.gemfile @@ -1,9 +1,9 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gemspec path: '../' +gemspec path: "../" -gem 'activerecord', '~> 6.0.0' -gem 'actionpack', '~> 6.0.0' +gem "activerecord", "~> 6.0.0" +gem "actionpack", "~> 6.0.0" gem "sqlite3", "~> 1.4" eval_gemfile "common.rb" diff --git a/gemfiles/rails6.1.gemfile b/gemfiles/rails6.1.gemfile index 002cf71..ebc4827 100644 --- a/gemfiles/rails6.1.gemfile +++ b/gemfiles/rails6.1.gemfile @@ -1,9 +1,9 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gemspec path: '../' +gemspec path: "../" -gem 'activerecord', '~> 6.1.0' -gem 'actionpack', '~> 6.1.0' +gem "activerecord", "~> 6.1.0" +gem "actionpack", "~> 6.1.0" gem "sqlite3", "~> 1.4" eval_gemfile "common.rb" diff --git a/gemfiles/rails7.0.gemfile b/gemfiles/rails7.0.gemfile index b1a8c35..c6b2986 100644 --- a/gemfiles/rails7.0.gemfile +++ b/gemfiles/rails7.0.gemfile @@ -1,9 +1,9 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gemspec path: '../' +gemspec path: "../" -gem 'activerecord', '~> 7.0.0' -gem 'actionpack', '~> 7.0.0' +gem "activerecord", "~> 7.0.0" +gem "actionpack", "~> 7.0.0" gem "sqlite3", "~> 1.4" eval_gemfile "common.rb" diff --git a/lib/property_sets.rb b/lib/property_sets.rb index 6be3d68..e6613ee 100644 --- a/lib/property_sets.rb +++ b/lib/property_sets.rb @@ -1,9 +1,9 @@ -require 'property_sets/property_set_model' -require 'property_sets/active_record_extension' -require 'property_sets/version' +require "property_sets/property_set_model" +require "property_sets/active_record_extension" +require "property_sets/version" begin - require 'property_sets/action_view_extension' + require "property_sets/action_view_extension" rescue LoadError end @@ -19,7 +19,7 @@ def self.ensure_property_set_class(association, owner_class_name) unless namespace.const_defined?(const_name, false) property_class = Class.new(parent_for_property_class(namespace, owner_class_name)) do include PropertySets::PropertySetModel::InstanceMethods - extend PropertySets::PropertySetModel::ClassMethods + extend PropertySets::PropertySetModel::ClassMethods end namespace.const_set(const_name, property_class) diff --git a/lib/property_sets/action_view_extension.rb b/lib/property_sets/action_view_extension.rb index e3eed1f..2676fac 100644 --- a/lib/property_sets/action_view_extension.rb +++ b/lib/property_sets/action_view_extension.rb @@ -1,4 +1,4 @@ -require 'action_view' +require "action_view" module ActionView module Helpers @@ -8,9 +8,9 @@ class PropertySetFormBuilderProxy def initialize(property_set, template, object_name, object) @property_set = property_set - @template = template - @object_name = object_name - @object = object + @template = template + @object_name = object_name + @object = object end def check_box(property, options = {}, checked_value = "1", unchecked_value = "0") @@ -23,7 +23,7 @@ def check_box(property, options = {}, checked_value = "1", unchecked_value = "0" def radio_button(property, checked_value = "1", options = {}) options[:id] ||= "#{object_name}_#{property_set}_#{property}_#{checked_value}" options = prepare_options(property, options) do |properties| - properties.send("#{property}") == checked_value + properties.send(property.to_s) == checked_value end template.radio_button(object_name, property, checked_value, options) end @@ -34,7 +34,7 @@ def text_field(property, options = {}) def hidden_field(property, options = {}) options = prepare_id_name(property, options) - unless options.keys.include?(:value) + unless options.key?(:value) options[:value] = cast_boolean(options[:object].send(property_set).send(property)) end template.hidden_field(object_name, property, options) @@ -43,7 +43,7 @@ def hidden_field(property, options = {}) def select(property, choices, options = {}, html_options = {}) options = prepare_id_name(property, options) current_value = options[:object].send(property_set).send(property) - template.select("#{object_name}[#{property_set}]", property, choices, { :selected => current_value }, html_options ) + template.select("#{object_name}[#{property_set}]", property, choices, {selected: current_value}, html_options) end private @@ -52,9 +52,9 @@ def prepare_id_name(property, options) throw "Invalid options type #{options.inspect}" unless options.is_a?(Hash) options.clone.tap do |prepared_options| - prepared_options[:object] = object || fetch_target_object - prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}" - prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]" + prepared_options[:object] = object || fetch_target_object + prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}" + prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]" end end @@ -75,18 +75,16 @@ def prepare_options(property, options, &block) def cast_boolean(value) case value - when TrueClass then '1' - when FalseClass then '0' + when TrueClass then "1" + when FalseClass then "0" else value end end - end def property_set(identifier) PropertySetFormBuilderProxy.new(identifier, @template, object_name, object) end - end end end diff --git a/lib/property_sets/active_record_extension.rb b/lib/property_sets/active_record_extension.rb index e77fe41..3591415 100644 --- a/lib/property_sets/active_record_extension.rb +++ b/lib/property_sets/active_record_extension.rb @@ -1,6 +1,5 @@ -require 'active_record' -require 'property_sets/casting' -require 'set' +require "active_record" +require "property_sets/casting" module PropertySets module ActiveRecordExtension @@ -9,20 +8,20 @@ module ClassMethods def property_set(association, options = {}, &block) unless include?(PropertySets::ActiveRecordExtension::InstanceMethods) - self.send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods) + send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods) cattr_accessor :property_set_index self.property_set_index = Set.new end - raise "Invalid association name, letters only" unless association.to_s =~ /[a-z]+/ + raise "Invalid association name, letters only" unless /[a-z]+/.match?(association.to_s) exists = property_set_index.include?(association) - self.property_set_index << association + property_set_index << association # eg AccountSetting - this IS idempotent property_class = PropertySets.ensure_property_set_class( association, - options.delete(:owner_class_name) || self.name + options.delete(:owner_class_name) || name ) # eg property :is_awesome @@ -32,17 +31,17 @@ def property_set(association, options = {}, &block) property_class.table_name = tb_name if tb_name hash_opts = { - :class_name => property_class.name, - :autosave => true, - :dependent => :destroy, - :inverse_of => self.name.demodulize.underscore.to_sym, + class_name: property_class.name, + autosave: true, + dependent: :destroy, + inverse_of: name.demodulize.underscore.to_sym }.merge(options) # TODO: should check options are compatible? warn? raise? - reflection = self.reflections[association.to_s] # => ActiveRecord::Reflection::HasManyReflection + reflection = reflections[association.to_s] # => ActiveRecord::Reflection::HasManyReflection reflection.options.merge! options if reflection && !options.empty? - unless exists then # makes has_many idempotent... + unless exists # makes has_many idempotent... has_many association, **hash_opts do # keep this damn block! -- creates association_module below end @@ -51,23 +50,23 @@ def property_set(association, options = {}, &block) # stolen/adapted from AR's collection_association.rb #define_extensions module_name = "#{association.to_s.camelize}AssociationExtension" - association_module = self.const_get module_name + association_module = const_get module_name association_module.module_eval do include PropertySets::ActiveRecordExtension::AssociationExtensions property_class.keys.each do |key| - raise "Invalid property key #{key}" if self.respond_to?(key) + raise "Invalid property key #{key}" if respond_to?(key) # Reports the coerced truth value of the property define_method "#{key}?" do - type = property_class.type(key) + type = property_class.type(key) value = lookup_value(type, key) - ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase) + !["false", "0", "", "off", "n"].member?(value.to_s.downcase) end # Returns the value of the property - define_method "#{key}" do + define_method key.to_s do type = property_class.type(key) lookup_value(type, key) end @@ -100,10 +99,10 @@ def get(keys = []) association_class.keys & keys.map(&:to_s) end - property_pairs = property_keys.map do |name| + property_pairs = property_keys.flat_map do |name| value = lookup_value(association_class.type(name), name) [name, value] - end.flatten(1) + end HashWithIndifferentAccess[*property_pairs] end @@ -140,7 +139,7 @@ def disable(arg) end def build_default(arg) - build(:name => arg.to_s, :value => association_class.raw_default(arg)) + build(name: arg.to_s, value: association_class.raw_default(arg)) end def lookup_without_default(arg) @@ -150,7 +149,7 @@ def lookup_without_default(arg) def lookup_value(type, key) serialized = property_serialized?(key) - if instance = lookup_without_default(key) + if (instance = lookup_without_default(key)) instance.value_serialized = serialized PropertySets::Casting.read(type, instance.value) else @@ -165,7 +164,7 @@ def lookup_value(type, key) # The finder method which returns the property if present, otherwise a new instance with defaults def lookup(arg) - instance = lookup_without_default(arg) + instance = lookup_without_default(arg) instance ||= build_default(arg) instance.value_serialized = property_serialized?(arg) @@ -179,7 +178,7 @@ def lookup(arg) # It does not have the side effect of adding a new setting object. def lookup_or_default(arg) instance = lookup_without_default(arg) - instance ||= association_class.new(:value => association_class.raw_default(arg)) + instance ||= association_class.new(value: association_class.raw_default(arg)) instance.value_serialized = property_serialized?(arg) instance end @@ -194,18 +193,18 @@ def update(attributes) update_property_set_attributes(attributes) super end - alias update_attributes update + alias_method :update_attributes, :update def update!(attributes) update_property_set_attributes(attributes) super end - alias update_attributes! update! + alias_method :update_attributes!, :update! def update_property_set_attributes(attributes) if attributes && self.class.property_set_index.any? self.class.property_set_index.each do |property_set| - if property_set_hash = attributes.delete(property_set) + if (property_set_hash = attributes.delete(property_set)) send(property_set).set(property_set_hash, true) end end @@ -214,7 +213,7 @@ def update_property_set_attributes(attributes) def update_columns(attributes) if delegated_property_sets? - attributes = attributes.reject{|k,_| self.class.delegated_property_set_attributes.include?(k.to_s) } + attributes = attributes.reject { |k, _| self.class.delegated_property_set_attributes.include?(k.to_s) } end super attributes @@ -241,7 +240,6 @@ def filter_delegated_property_set_attributes(attribute_names) attribute_names end end - end end diff --git a/lib/property_sets/casting.rb b/lib/property_sets/casting.rb index 7054a15..f017f8e 100644 --- a/lib/property_sets/casting.rb +++ b/lib/property_sets/casting.rb @@ -1,8 +1,8 @@ -require 'json' +require "json" module PropertySets module Casting - FALSE = [ "false", "0", "", "off", "n" ] + FALSE_VALUES = ["false", "0", "", "off", "n"] class << self def read(type, value) @@ -53,7 +53,7 @@ def deserialize(value) private def false?(value) - FALSE.include?(value.to_s.downcase) + FALSE_VALUES.include?(value.to_s.downcase) end end end diff --git a/lib/property_sets/delegator.rb b/lib/property_sets/delegator.rb index 67dcf25..a196ea4 100644 --- a/lib/property_sets/delegator.rb +++ b/lib/property_sets/delegator.rb @@ -65,7 +65,7 @@ def delegate_to_property_set(setname, mappings) # These are not database columns and should not be included in queries but # using the attributes API is the only way to track changes in the main model if respond_to?(:user_provided_columns) - self.user_provided_columns.reject!{|k,_| delegated_property_set_attributes.include?(k.to_s) } + user_provided_columns.reject! { |k, _| delegated_property_set_attributes.include?(k.to_s) } end end end diff --git a/lib/property_sets/property_set_model.rb b/lib/property_sets/property_set_model.rb index ac7fe0a..9fcfe7d 100644 --- a/lib/property_sets/property_set_model.rb +++ b/lib/property_sets/property_set_model.rb @@ -1,22 +1,22 @@ -require 'active_support' +require "active_support" module PropertySets module PropertySetModel # https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html COLUMN_TYPE_LIMITS = { - 'tinyblob' => 255, # 2^8 - 1 - 'tinytext' => 255, - 'blob' => 65535, # 2^16 - 1 - 'text' => 65535, - 'mediumblob' => 16777215, # 2^24 - 1 - 'mediumtext' => 16777215, - 'longblob' => 4294967295, # 2^32 - 1 - 'longtext' => 4294967295, + "tinyblob" => 255, # 2^8 - 1 + "tinytext" => 255, + "blob" => 65535, # 2^16 - 1 + "text" => 65535, + "mediumblob" => 16777215, # 2^24 - 1 + "mediumtext" => 16777215, + "longblob" => 4294967295, # 2^32 - 1 + "longtext" => 4294967295 }.freeze module InstanceMethods def false? - [ "false", "0", "", "off", "n" ].member?(value.to_s.downcase) + ["false", "0", "", "off", "n"].member?(value.to_s.downcase) end def true? @@ -93,7 +93,7 @@ def owner_class_instance end def value_column_limit - column = self.class.columns_hash.fetch('value') + column = self.class.columns_hash.fetch("value") # use sql_type because type returns :text for all text types regardless of length column.limit || COLUMN_TYPE_LIMITS.fetch(column.sql_type) @@ -102,9 +102,9 @@ def value_column_limit module ClassMethods def self.extended(base) - base.validate :validate_format_of_name - base.validate :validate_length_of_serialized_data - base.before_create :coerce_value + base.validate :validate_format_of_name + base.validate :validate_length_of_serialized_data + base.before_create :coerce_value base.attr_accessible :name, :value if defined?(ProtectedAttributes) end @@ -139,10 +139,10 @@ def protected?(key) def owner_class=(owner_class_name) @owner_class_sym = owner_class_name.to_s.demodulize.underscore.to_sym - belongs_to owner_class_sym, class_name: owner_class_name - validates_presence_of owner_class_sym, class_name: owner_class_name - validates_uniqueness_of :name, :scope => owner_class_key_sym, :case_sensitive => false - attr_accessible owner_class_key_sym, owner_class_sym if defined?(ProtectedAttributes) + belongs_to owner_class_sym, class_name: owner_class_name + validates_presence_of owner_class_sym, class_name: owner_class_name + validates_uniqueness_of :name, scope: owner_class_key_sym, case_sensitive: false + attr_accessible owner_class_key_sym, owner_class_sym if defined?(ProtectedAttributes) end def owner_assoc=(association) @@ -161,6 +161,5 @@ def owner_class_key_sym "#{owner_class_sym}_id".to_sym end end - end end diff --git a/property_sets.gemspec b/property_sets.gemspec index d135a7d..1c89b5c 100644 --- a/property_sets.gemspec +++ b/property_sets.gemspec @@ -1,12 +1,12 @@ -require './lib/property_sets/version' +require "./lib/property_sets/version" Gem::Specification.new "property_sets", PropertySets::VERSION do |s| - s.summary = "Property sets for ActiveRecord." + s.summary = "Property sets for ActiveRecord." s.description = "This gem is an ActiveRecord extension which provides a convenient interface for managing per row properties." - s.authors = ["Morten Primdahl"] - s.email = 'primdahl@me.com' - s.homepage = 'http://github.com/zendesk/property_sets' - s.license = 'Apache License Version 2.0' + s.authors = ["Morten Primdahl"] + s.email = "primdahl@me.com" + s.homepage = "http://github.com/zendesk/property_sets" + s.license = "Apache License Version 2.0" s.required_ruby_version = ">= 2.7" @@ -15,9 +15,10 @@ Gem::Specification.new "property_sets", PropertySets::VERSION do |s| s.add_development_dependency("bump") s.add_development_dependency("rake") - s.add_development_dependency('actionpack') - s.add_development_dependency('rspec') - s.add_development_dependency('byebug') + s.add_development_dependency("actionpack") + s.add_development_dependency("rspec") + s.add_development_dependency("standard") + s.add_development_dependency("byebug") s.files = `git ls-files lib`.split("\n") s.license = "MIT" diff --git a/spec/casting_spec.rb b/spec/casting_spec.rb index 14cdbdc..fab4d3c 100644 --- a/spec/casting_spec.rb +++ b/spec/casting_spec.rb @@ -1,31 +1,31 @@ -require 'spec_helper' -require 'property_sets/casting' +require "spec_helper" +require "property_sets/casting" describe PropertySets::Casting do describe "#read" do it "return nil when given value nil regardless of type" do expect(PropertySets::Casting.read(:string, nil)).to be_nil - expect(PropertySets::Casting.read(:hello, nil)) .to be_nil + expect(PropertySets::Casting.read(:hello, nil)).to be_nil end it "leave serialized data alone" do - expect(PropertySets::Casting.read(:serialized, [1, 2, 3])).to eq([1,2,3]) + expect(PropertySets::Casting.read(:serialized, [1, 2, 3])).to eq([1, 2, 3]) end it "reads boolean" do - expect(PropertySets::Casting.read(:boolean, "true")) .to be true - expect(PropertySets::Casting.read(:boolean, "1")) .to be true + expect(PropertySets::Casting.read(:boolean, "true")).to be true + expect(PropertySets::Casting.read(:boolean, "1")).to be true expect(PropertySets::Casting.read(:boolean, "something")).to be true - expect(PropertySets::Casting.read(:boolean, "on")) .to be true - expect(PropertySets::Casting.read(:boolean, true)) .to be true - expect(PropertySets::Casting.read(:boolean, 1111)) .to be true + expect(PropertySets::Casting.read(:boolean, "on")).to be true + expect(PropertySets::Casting.read(:boolean, true)).to be true + expect(PropertySets::Casting.read(:boolean, 1111)).to be true end end describe "#write" do it "return nil when given value nil regardless of type" do expect(PropertySets::Casting.write(:string, nil)).to be_nil - expect(PropertySets::Casting.write(:hello, nil)) .to be_nil + expect(PropertySets::Casting.write(:hello, nil)).to be_nil end it "convert time instances to UTC" do @@ -38,16 +38,16 @@ end it "convert random things to booleans" do - expect(PropertySets::Casting.write(:boolean, 1)) .to eq("1") - expect(PropertySets::Casting.write(:boolean, true)) .to eq("1") + expect(PropertySets::Casting.write(:boolean, 1)).to eq("1") + expect(PropertySets::Casting.write(:boolean, true)).to eq("1") expect(PropertySets::Casting.write(:boolean, "dfsdff")).to eq("1") - expect(PropertySets::Casting.write(:boolean, "")) .to eq("0") - expect(PropertySets::Casting.write(:boolean, nil)) .to be_nil + expect(PropertySets::Casting.write(:boolean, "")).to eq("0") + expect(PropertySets::Casting.write(:boolean, nil)).to be_nil expect(PropertySets::Casting.write(:boolean, false)).to eq("0") - expect(PropertySets::Casting.write(:boolean, 0)) .to eq("0") + expect(PropertySets::Casting.write(:boolean, 0)).to eq("0") expect(PropertySets::Casting.write(:boolean, "off")).to eq("0") - expect(PropertySets::Casting.write(:boolean, "n")) .to eq("0") + expect(PropertySets::Casting.write(:boolean, "n")).to eq("0") end it "leave serialized data alone for the record to deal with" do diff --git a/spec/delegator_spec.rb b/spec/delegator_spec.rb index 1096da4..e65674e 100644 --- a/spec/delegator_spec.rb +++ b/spec/delegator_spec.rb @@ -1,8 +1,8 @@ -require 'spec_helper' +require "spec_helper" describe PropertySets::Delegator do - let(:account) { Parent::Account.create(:name => "Name") } - let(:default) { 'skep' } + let(:account) { Parent::Account.create(name: "Name") } + let(:default) { "skep" } describe "read" do it "not add a property" do @@ -15,21 +15,21 @@ end it "delegate read to property value" do - account.settings.hep = 'new' - expect(account.old).to eq('new') + account.settings.hep = "new" + expect(account.old).to eq("new") end end describe "write" do it "add a property" do - account.old = 'new' + account.old = "new" expect(account.settings.size).to eq(1) end it "delegate write" do - account.old = 'new' - expect(account.settings.hep).to eq('new') - expect(account.old).to eq('new') + account.old = "new" + expect(account.settings.hep).to eq("new") + expect(account.old).to eq("new") end end diff --git a/spec/inheritance_spec.rb b/spec/inheritance_spec.rb index 7ac1cb6..d1da522 100644 --- a/spec/inheritance_spec.rb +++ b/spec/inheritance_spec.rb @@ -1,13 +1,13 @@ -require 'active_support' -require 'active_record' -require 'property_sets' +require "active_support" +require "active_record" +require "property_sets" yaml_config = "spec/support/database.yml" ActiveRecord::Base.configurations = begin - YAML.safe_load(IO.read(yaml_config), aliases: true) - rescue ArgumentError - YAML.safe_load(IO.read(yaml_config)) - end + YAML.safe_load(IO.read(yaml_config), aliases: true) +rescue ArgumentError + YAML.safe_load(IO.read(yaml_config)) +end class AbstractUnshardedModel < ActiveRecord::Base self.abstract_class = true diff --git a/spec/property_sets_spec.rb b/spec/property_sets_spec.rb index 6887251..ae65797 100644 --- a/spec/property_sets_spec.rb +++ b/spec/property_sets_spec.rb @@ -1,13 +1,13 @@ -require 'spec_helper' +require "spec_helper" old, $-w = $-w, nil # sqlite type differences: PropertySets::PropertySetModel::COLUMN_TYPE_LIMITS = - PropertySets::PropertySetModel::COLUMN_TYPE_LIMITS.merge('varchar' => 65535) + PropertySets::PropertySetModel::COLUMN_TYPE_LIMITS.merge("varchar" => 65535) $-w = old describe PropertySets do - let(:account) { Parent::Account.create(:name => "Name") } + let(:account) { Parent::Account.create(name: "Name") } let(:relation) { Parent::Account.reflections["settings"] } it "construct the container class" do @@ -17,7 +17,7 @@ end it "register the property sets used on a class" do - %i(settings texts validations typed_data).each do |name| + %i[settings texts validations typed_data].each do |name| expect(Parent::Account.property_set_index).to include(name) end end @@ -33,26 +33,26 @@ it "allow the owner class to be customized" do (Flux = Class.new(ActiveRecord::Base)).property_set(:blot, { - :owner_class_name => 'Foobar' + owner_class_name: "Foobar" }) { property :test } expect(defined?(FoobarBlot)).to be_truthy end it "pass-through any options from the second parameter" do - class AnotherThing < MainDatabase + class AnotherThing < MainDatabase # standard:disable Lint/ConstantDefinitionInBlock: self.table_name = "things" # cheat and reuse things table end - AnotherThing.property_set(:settings, :extend => Parent::Account::Woot, - :table_name => "thing_settings") + AnotherThing.property_set(:settings, extend: Parent::Account::Woot, + table_name: "thing_settings") expect(AnotherThing.new.settings.extensions).to include(::Parent::Account::Woot) end end RSpec.shared_examples "different account models" do |account_klass| - let(:account) { account_klass.create(:name => "Name") } + let(:account) { account_klass.create(name: "Name") } let(:relation) { account_klass.reflections["settings"] } it "support protecting attributes" do @@ -75,7 +75,7 @@ class AnotherThing < MainDatabase it "be empty on a new account" do expect(account.settings).to be_empty - expect(account.texts) .to be_empty + expect(account.texts).to be_empty expect(account.texts.foo?).to be false expect(account.texts.bar?).to be false @@ -85,33 +85,33 @@ class AnotherThing < MainDatabase end it "respond with defaults" do - expect(account.settings.bar?) .to be false - expect(account.settings.bar) .to be_nil - expect(account.settings.hep?) .to be true - expect(account.settings.hep) .to eq('skep') - expect(account.settings.bool_nil) .to be_nil - expect(account.settings.bool_nil2) .to be_nil + expect(account.settings.bar?).to be false + expect(account.settings.bar).to be_nil + expect(account.settings.hep?).to be true + expect(account.settings.hep).to eq("skep") + expect(account.settings.bool_nil).to be_nil + expect(account.settings.bool_nil2).to be_nil expect(account.settings.bool_false).to be false - expect(account.settings.bool_true) .to be true + expect(account.settings.bool_true).to be true end it "be flexible when fetching property data" do - expect(account.settings.association_class.default(:hep)) .to eq('skep') - expect(account.settings.association_class.default('hep')).to eq('skep') + expect(account.settings.association_class.default(:hep)).to eq("skep") + expect(account.settings.association_class.default("hep")).to eq("skep") end - describe 'querying for a setting that does not exist' do + describe "querying for a setting that does not exist" do before do expect(account.settings).to eq([]) expect(account.settings.hep?).to be true end - it 'not add a new setting' do + it "not add a new setting" do expect(account.settings).to eq([]) end - it 'give back the default value' do - expect(account.settings.hep).to eq('skep') + it "give back the default value" do + expect(account.settings.hep).to eq("skep") end end @@ -122,8 +122,8 @@ class AnotherThing < MainDatabase settings_klass = Object.const_get("#{account_klass}Setting") s = settings_klass.new(account.model_name.element.to_sym => account) - valids = %w(hello hel_lo hell0) + [:hello] - invalids = %w(_hello) + valids = %w[hello hel_lo hell0] + [:hello] + invalids = %w[_hello] valids.each do |valid| s.name = valid @@ -137,17 +137,17 @@ class AnotherThing < MainDatabase end it "validate uniqueness of settings" do - account.settings.create!(:name => "unique") + account.settings.create!(name: "unique") expect { - account.settings.create!(:name => "unique") + account.settings.create!(name: "unique") }.to raise_error(ActiveRecord::RecordInvalid, /Name has already been taken/) end it "be creatable using the = operator" do expect(account.settings.foo?).to be false - [ "1", "2" ].each do |value| + ["1", "2"].each do |value| expect(account.settings.foo = value).to be_truthy - expect(account.settings.foo?) .to be true + expect(account.settings.foo?).to be true expect(account.settings.size).to eq(1) end @@ -173,8 +173,8 @@ class AnotherThing < MainDatabase end it "reference the owner instance when constructing a new record ...on a new record" do - account = Parent::Account.new(:name => "New") - record = account.settings.lookup(:baz) + account = Parent::Account.new(name: "New") + record = account.settings.lookup(:baz) expect(record).to be_new_record expect(record.account).to eq(account) @@ -189,7 +189,7 @@ class AnotherThing < MainDatabase end describe "#get" do - before { account.settings.set(:baz => "456") } + before { account.settings.set(baz: "456") } it "fetch property pairs with string arguments" do expect(account.settings.lookup_without_default(:baz)).to be_truthy @@ -202,7 +202,7 @@ class AnotherThing < MainDatabase it "return all property pairs if no arguments are provided" do expect(account.settings.get.keys.sort).to eq( - %w(bar baz bool_false bool_nil bool_nil2 bool_true foo hep pro).sort + %w[bar baz bool_false bool_nil bool_nil2 bool_true foo hep pro].sort ) end @@ -220,7 +220,7 @@ class AnotherThing < MainDatabase end it "return serialized values" do - account.typed_data.set(:serialized_prop => [1, 2]) + account.typed_data.set(serialized_prop: [1, 2]) expect(account.typed_data.lookup_without_default(:serialized_prop)).to be_truthy expect(account.typed_data.get([:serialized_prop])).to eq("serialized_prop" => [1, 2]) end @@ -231,38 +231,38 @@ class AnotherThing < MainDatabase expect(account.settings.foo?).to be_falsy expect(account.settings.bar?).to be_falsy - account.settings.set(:foo => "123", :bar => "456") + account.settings.set(foo: "123", bar: "456") expect(account.settings.foo?).to be_truthy expect(account.settings.bar?).to be_truthy end it "convert string keys to symbols to ensure consistent lookup" do - account.settings.set(:foo => "123") + account.settings.set(foo: "123") account.settings.set("foo" => "456") expect(account.save!).to be true end it "work identically for new and existing owner objects" do - [ account, Parent::Account.new(:name => "Mibble") ].each do |account| - account.settings.set(:foo => "123", :bar => "456") + [account, Parent::Account.new(name: "Mibble")].each do |account| + account.settings.set(foo: "123", bar: "456") expect(account.settings.size).to eq(2) - expect(account.settings.foo) .to eq("123") - expect(account.settings.bar) .to eq("456") + expect(account.settings.foo).to eq("123") + expect(account.settings.bar).to eq("456") - account.settings.set(:bar => "789", :baz => "012") + account.settings.set(bar: "789", baz: "012") expect(account.settings.size).to eq(3) - expect(account.settings.foo) .to eq("123") - expect(account.settings.bar) .to eq("789") - expect(account.settings.baz) .to eq("012") + expect(account.settings.foo).to eq("123") + expect(account.settings.bar).to eq("789") + expect(account.settings.baz).to eq("012") end end it "be updateable as AR nested attributes" do expect( - account.texts_attributes = [{ :name => "foo", :value => "1" }, { :name => "bar", :value => "0" }] + account.texts_attributes = [{name: "foo", value: "1"}, {name: "bar", value: "0"}] ).to be_truthy account.save! @@ -270,9 +270,9 @@ class AnotherThing < MainDatabase expect(account.texts.foo).to eq("1") expect(account.texts.bar).to eq("0") - account.update_attributes!(:texts_attributes => [ - { :id => account.texts.lookup(:foo).id, :name => "foo", :value => "0" }, - { :id => account.texts.lookup(:bar).id, :name => "bar", :value => "1" } + account.update_attributes!(texts_attributes: [ + {id: account.texts.lookup(:foo).id, name: "foo", value: "0"}, + {id: account.texts.lookup(:bar).id, name: "bar", value: "1"} ]) expect(account.texts.foo).to eq("0") @@ -289,8 +289,8 @@ class AnotherThing < MainDatabase expect(account.settings.pro?).to be false account.update_attributes!( - :name => "Kim", - :settings => { :foo => "1", :baz => "0", :pro => "1" } + name: "Kim", + settings: {foo: "1", baz: "0", pro: "1"} ) account.reload @@ -415,18 +415,17 @@ class AnotherThing < MainDatabase describe "update_columns for forwarded method" do it "does not write to a missing column" do - account.update_columns(name: 'test', old: "it works!") + account.update_columns(name: "test", old: "it works!") expect(account.previous_changes).to_not include("old") end it "does not prevent other non-delegated property set models from updating" do - thing = Thing.create(name: 'test') - expect(thing.update_columns(name: 'it works')).to be + thing = Thing.create(name: "test") + expect(thing.update_columns(name: "it works")).to be end end describe "typed columns" do - it "typecast the default value" do expect(account.typed_data.association_class.default(:default_prop)).to eq(123) end @@ -479,11 +478,11 @@ class AnotherThing < MainDatabase describe "serialized data" do it "store data in json" do - value = {:a => 1, :b => 2} + value = {a: 1, b: 2} account.typed_data.serialized_prop = value account.save! account.reload - expect(account.typed_data.serialized_prop).to eq('a' => 1, 'b' => 2) + expect(account.typed_data.serialized_prop).to eq("a" => 1, "b" => 2) end it "retrieve default values from JSON" do @@ -503,7 +502,7 @@ class AnotherThing < MainDatabase end it "allow for destructive operators" do - value = {:a => 1, :b => 2} + value = {a: 1, b: 2} account.typed_data.serialized_prop = value account.typed_data.serialized_prop[:c] = 3 expect(account.typed_data.serialized_prop[:c]).to eq(3) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 547def0..64a15d6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,26 +1,26 @@ -require 'bundler/setup' +require "bundler/setup" -require 'active_support' -require 'active_record' -require 'active_record/fixtures' +require "active_support" +require "active_record" +require "active_record/fixtures" ENV["RAILS_ENV"] = "test" LEGACY_CONNECTION_HANDLING = (ENV["LEGACY_CONNECTION_HANDLING"] == "true") case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" -when '7.0' +when "7.0" ActiveRecord.legacy_connection_handling = LEGACY_CONNECTION_HANDLING -when '6.1' +when "6.1" ActiveRecord::Base.legacy_connection_handling = LEGACY_CONNECTION_HANDLING end -require 'property_sets' -require 'property_sets/delegator' +require "property_sets" +require "property_sets/delegator" -require 'support/database_config' -require 'support/models' -require 'support/database_migrations' +require "support/database_config" +require "support/models" +require "support/database_migrations" I18n.enforce_available_locales = false @@ -47,7 +47,7 @@ module ActiveRecord module Associations class CollectionProxy def scoping - raise 'CollectionProxy delegates unknown methods to target (association_class) via method_missing, wrapping the call with `scoping`. Instead, call the method directly on the association_class!' + raise "CollectionProxy delegates unknown methods to target (association_class) via method_missing, wrapping the call with `scoping`. Instead, call the method directly on the association_class!" end end end diff --git a/spec/support/database_config.rb b/spec/support/database_config.rb index 4d7f2ca..8a39173 100644 --- a/spec/support/database_config.rb +++ b/spec/support/database_config.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true config = { - :test => { - :test_database => { - :adapter => "sqlite3", - :database => ":memory:", + test: { + test_database: { + adapter: "sqlite3", + database: ":memory:" }, - :test_alt_database => { - :adapter => "sqlite3", - :database => ":memory:" + test_alt_database: { + adapter: "sqlite3", + database: ":memory:" } } } diff --git a/spec/support/database_migrations.rb b/spec/support/database_migrations.rb index edd4fbe..c76335b 100644 --- a/spec/support/database_migrations.rb +++ b/spec/support/database_migrations.rb @@ -1,10 +1,10 @@ # setup database -require 'active_record' -ActiveRecord::Base.logger = Logger.new(STDOUT) +require "active_record" +ActiveRecord::Base.logger = Logger.new($stdout) ActiveRecord::Base.logger.level = Logger::ERROR ActiveRecord::Base.establish_connection(:test_database) -ActiveRecord::Base.connection.execute('select 1') +ActiveRecord::Base.connection.execute("select 1") ActiveRecord::Migration.verbose = false @@ -14,93 +14,93 @@ def connection end def change - create_table "account_benchmark_settings", :force => true do |t| - t.integer "account_id" - t.string "name" - t.string "value" + create_table "account_benchmark_settings", force: true do |t| + t.integer "account_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_benchmark_settings, [ :account_id, :name ], :unique => true + add_index :account_benchmark_settings, [:account_id, :name], unique: true - create_table "accounts", :force => true do |t| - t.string "name" + create_table "accounts", force: true do |t| + t.string "name" t.datetime "created_at" t.datetime "updated_at" end - create_table "account_alt_dbs", :force => true do |t| - t.string "name" + create_table "account_alt_dbs", force: true do |t| + t.string "name" t.datetime "created_at" t.datetime "updated_at" end - create_table "account_settings", :force => true do |t| - t.integer "account_id" - t.string "name" - t.string "value" + create_table "account_settings", force: true do |t| + t.integer "account_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_settings, [ :account_id, :name ], :unique => true + add_index :account_settings, [:account_id, :name], unique: true - create_table "account_texts", :force => true do |t| - t.integer "account_id" - t.string "name" - t.string "value" + create_table "account_texts", force: true do |t| + t.integer "account_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_texts, [ :account_id, :name ], :unique => true + add_index :account_texts, [:account_id, :name], unique: true - create_table "account_typed_data", :force => true do |t| - t.integer "account_id" - t.string "name" - t.string "value" + create_table "account_typed_data", force: true do |t| + t.integer "account_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_typed_data, [ :account_id, :name ], :unique => true + add_index :account_typed_data, [:account_id, :name], unique: true - create_table "account_validations", :force => true do |t| - t.integer "account_id" - t.string "name" - t.string "value" + create_table "account_validations", force: true do |t| + t.integer "account_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_validations, [ :account_id, :name ], :unique => true + add_index :account_validations, [:account_id, :name], unique: true - create_table "account_tiny_texts", :force => true do |t| - t.integer "account_id" - t.string "name" - t.text "value", :limit => (2**8 - 1) + create_table "account_tiny_texts", force: true do |t| + t.integer "account_id" + t.string "name" + t.text "value", limit: (2**8 - 1) t.datetime "created_at" t.datetime "updated_at" end - add_index :account_tiny_texts, [ :account_id, :name ], :unique => true + add_index :account_tiny_texts, [:account_id, :name], unique: true - create_table "things", :force => true do |t| - t.string "name" + create_table "things", force: true do |t| + t.string "name" t.datetime "created_at" t.datetime "updated_at" end - create_table "thing_settings", :force => true do |t| - t.integer "thing_id" - t.string "name" - t.string "value" + create_table "thing_settings", force: true do |t| + t.integer "thing_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :thing_settings, [ :thing_id, :name ], :unique => true + add_index :thing_settings, [:thing_id, :name], unique: true end end @@ -112,55 +112,55 @@ def connection end def change - create_table "account_alt_db_settings", :force => true do |t| - t.integer "account_alt_db_id" - t.string "name" - t.string "value" + create_table "account_alt_db_settings", force: true do |t| + t.integer "account_alt_db_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_alt_db_settings, [ :account_alt_db_id, :name ], :unique => true + add_index :account_alt_db_settings, [:account_alt_db_id, :name], unique: true - create_table "account_alt_db_texts", :force => true do |t| - t.integer "account_alt_db_id" - t.string "name" - t.string "value" + create_table "account_alt_db_texts", force: true do |t| + t.integer "account_alt_db_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_alt_db_texts, [ :account_alt_db_id, :name ], :unique => true + add_index :account_alt_db_texts, [:account_alt_db_id, :name], unique: true - create_table "account_alt_db_typed_data", :force => true do |t| - t.integer "account_alt_db_id" - t.string "name" - t.string "value" + create_table "account_alt_db_typed_data", force: true do |t| + t.integer "account_alt_db_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_alt_db_typed_data, [ :account_alt_db_id, :name ], :unique => true + add_index :account_alt_db_typed_data, [:account_alt_db_id, :name], unique: true - create_table "account_alt_db_validations", :force => true do |t| - t.integer "account_alt_db_id" - t.string "name" - t.string "value" + create_table "account_alt_db_validations", force: true do |t| + t.integer "account_alt_db_id" + t.string "name" + t.string "value" t.datetime "created_at" t.datetime "updated_at" end - add_index :account_alt_db_validations, [ :account_alt_db_id, :name ], :unique => true + add_index :account_alt_db_validations, [:account_alt_db_id, :name], unique: true - create_table "account_alt_db_tiny_texts", :force => true do |t| - t.integer "account_alt_db_id" - t.string "name" - t.text "value", :limit => (2**8 - 1) + create_table "account_alt_db_tiny_texts", force: true do |t| + t.integer "account_alt_db_id" + t.string "name" + t.text "value", limit: (2**8 - 1) t.datetime "created_at" t.datetime "updated_at" end - add_index :account_alt_db_tiny_texts, [ :account_alt_db_id, :name ], :unique => true + add_index :account_alt_db_tiny_texts, [:account_alt_db_id, :name], unique: true end end diff --git a/spec/support/models.rb b/spec/support/models.rb index 64c07b9..56da5cd 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true -require_relative 'acts_like_an_integer' + +require_relative "acts_like_an_integer" if LEGACY_CONNECTION_HANDLING class MainDatabase < ActiveRecord::Base @@ -28,7 +29,7 @@ module Parent class Account < MainDatabase include PropertySets::Delegator - delegate_to_property_set :settings, :old => :hep + delegate_to_property_set :settings, old: :hep # nonsense module to use in options below, only used as a marker module Woot # doesn't actually seem to be used in AR4 ? @@ -38,16 +39,16 @@ module Woot # doesn't actually seem to be used in AR4 ? property :foo property :bar property :baz - property :hep, :default => 'skep' - property :pro, :protected => true - property :bool_true, :type => :boolean, :default => true - property :bool_false, :type => :boolean, :default => false - property :bool_nil, :type => :boolean, :default => nil + property :hep, default: "skep" + property :pro, protected: true + property :bool_true, type: :boolean, default: true + property :bool_false, type: :boolean, default: false + property :bool_nil, type: :boolean, default: nil end property_set :settings do # reopening should maintain `extend` above - property :bool_nil2, :type => :boolean + property :bool_nil2, type: :boolean end property_set :texts do @@ -61,21 +62,21 @@ module Woot # doesn't actually seem to be used in AR4 ? property :validated property :regular - validates_format_of :value, :with => /\d+/, :message => "BEEP", :if => lambda { |r| r.name.to_sym == :validated } + validates_format_of :value, with: /\d+/, message: "BEEP", if: lambda { |r| r.name.to_sym == :validated } end property_set :typed_data do - property :string_prop, :type => :string - property :datetime_prop, :type => :datetime - property :float_prop, :type => :float - property :int_prop, :type => :integer - property :serialized_prop, :type => :serialized - property :default_prop, :type => :integer, :default => ActsLikeAnInteger.new - property :serialized_prop_with_default, :type => :serialized, :default => "[]" + property :string_prop, type: :string + property :datetime_prop, type: :datetime + property :float_prop, type: :float + property :int_prop, type: :integer + property :serialized_prop, type: :serialized + property :default_prop, type: :integer, default: ActsLikeAnInteger.new + property :serialized_prop_with_default, type: :serialized, default: "[]" end property_set :tiny_texts do - property :serialized, :type => :serialized + property :serialized, type: :serialized end end end @@ -91,7 +92,7 @@ class AccountAltDb < MainDatabase self.property_sets_connection_class = AltDatabase - delegate_to_property_set :settings, :old => :hep + delegate_to_property_set :settings, old: :hep # nonsense module to use in options below, only used as a marker module Woot # doesn't actually seem to be used in AR4 ? @@ -101,16 +102,16 @@ module Woot # doesn't actually seem to be used in AR4 ? property :foo property :bar property :baz - property :hep, :default => 'skep' - property :pro, :protected => true - property :bool_true, :type => :boolean, :default => true - property :bool_false, :type => :boolean, :default => false - property :bool_nil, :type => :boolean, :default => nil + property :hep, default: "skep" + property :pro, protected: true + property :bool_true, type: :boolean, default: true + property :bool_false, type: :boolean, default: false + property :bool_nil, type: :boolean, default: nil end property_set :settings do # reopening should maintain `extend` above - property :bool_nil2, :type => :boolean + property :bool_nil2, type: :boolean end property_set :texts do @@ -124,21 +125,21 @@ module Woot # doesn't actually seem to be used in AR4 ? property :validated property :regular - validates_format_of :value, :with => /\d+/, :message => "BEEP", :if => lambda { |r| r.name.to_sym == :validated } + validates_format_of :value, with: /\d+/, message: "BEEP", if: lambda { |r| r.name.to_sym == :validated } end property_set :typed_data do - property :string_prop, :type => :string - property :datetime_prop, :type => :datetime - property :float_prop, :type => :float - property :int_prop, :type => :integer - property :serialized_prop, :type => :serialized - property :default_prop, :type => :integer, :default => ActsLikeAnInteger.new - property :serialized_prop_with_default, :type => :serialized, :default => "[]" + property :string_prop, type: :string + property :datetime_prop, type: :datetime + property :float_prop, type: :float + property :int_prop, type: :integer + property :serialized_prop, type: :serialized + property :default_prop, type: :integer, default: ActsLikeAnInteger.new + property :serialized_prop_with_default, type: :serialized, default: "[]" end property_set :tiny_texts do - property :serialized, :type => :serialized + property :serialized, type: :serialized end end end diff --git a/spec/view_extensions_spec.rb b/spec/view_extensions_spec.rb index bd77f82..49ba84d 100644 --- a/spec/view_extensions_spec.rb +++ b/spec/view_extensions_spec.rb @@ -1,32 +1,32 @@ -require 'spec_helper' +require "spec_helper" describe "property set view extensions" do def base_options { - :name => "#{object_name}[#{property_set}][#{property}]", - :id => "#{object_name}_#{property_set}_#{property}", - :object => object + name: "#{object_name}[#{property_set}][#{property}]", + id: "#{object_name}_#{property_set}_#{property}", + object: object } end let(:property_set) { :settings } - let(:property) { :active } - let(:object_name) { 'object_name' } - let(:object) { double('View object') } - let(:template) { double('Template') } - let(:builder) { ActionView::Helpers::FormBuilder.new(object_name, object, template, {}) } - let(:proxy) { builder.property_set(property_set) } + let(:property) { :active } + let(:object_name) { "object_name" } + let(:object) { double("View object") } + let(:template) { double("Template") } + let(:builder) { ActionView::Helpers::FormBuilder.new(object_name, object, template, {}) } + let(:proxy) { builder.property_set(property_set) } it "provide a form builder proxy" do expect(proxy).to be_a(ActionView::Helpers::FormBuilder::PropertySetFormBuilderProxy) expect(proxy.property_set).to eq(property_set) end - describe 'object is not available' do + describe "object is not available" do let(:builder) { ActionView::Helpers::FormBuilder.new(object_name, nil, template, {}) } it "fetch the target object when not available" do - allow(object).to receive(property_set).and_return(double('Fake property', property => 'value')) + 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) @@ -37,26 +37,26 @@ def base_options 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}?".to_sym => true) allow(object).to receive(property_set).and_return(settings) end it "build a checkbox with the proper parameters" do - expected_options = base_options.merge(:checked => true) - expect(template).to receive(:check_box).with(object_name, property, expected_options, '1', '0') + expected_options = base_options.merge(checked: true) + expect(template).to receive(:check_box).with(object_name, property, expected_options, "1", "0") proxy.check_box(property) end end 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}?".to_sym => false) allow(object).to receive(property_set).and_return(settings) end it "build a checkbox with the proper parameters" do - expected_options = base_options.merge(:checked => false) - expect(template).to receive(:check_box).with(object_name, property, expected_options, '1', '0') + expected_options = base_options.merge(checked: false) + expect(template).to receive(:check_box).with(object_name, property, expected_options, "1", "0") proxy.check_box(property) end end @@ -65,40 +65,40 @@ def base_options describe "#hidden_field" do describe "when the persisted value is not a boolean" do before do - settings = double('Fake property', property => 'persisted value') + settings = double("Fake property", property => "persisted value") allow(object).to receive(property_set).and_return(settings) end it "build a hidden field with the persisted value" do - expected_options = base_options.merge(:value => 'persisted value') + expected_options = base_options.merge(value: "persisted value") expect(template).to receive(:hidden_field).with(object_name, property, expected_options) proxy.hidden_field(property) end describe "and a value is provided" do it "build a hidden field with the provided value" do - expected_options = base_options.merge(:value => 'provided value') + expected_options = base_options.merge(value: "provided value") expect(template).to receive(:hidden_field).with(object_name, property, expected_options) - proxy.hidden_field(property, {:value => 'provided value'}) + proxy.hidden_field(property, {value: "provided value"}) end end end describe "when the persisted value is a boolean" do it "build a hidden field with cast boolean value if it is a boolean true" do - settings = double('Fake property', property => true) + settings = double("Fake property", property => true) allow(object).to receive(property_set).and_return(settings) - expected_options = base_options.merge(:value => '1') + expected_options = base_options.merge(value: "1") expect(template).to receive(:hidden_field).with(object_name, property, expected_options) proxy.hidden_field(property) end it "build a hidden field with cast boolean value if it is a boolean false" do - settings = double('Fake property', property => false) + settings = double("Fake property", property => false) allow(object).to receive(property_set).and_return(settings) - expected_options = base_options.merge(:value => '0') + expected_options = base_options.merge(value: "0") expect(template).to receive(:hidden_field).with(object_name, property, expected_options) proxy.hidden_field(property) end @@ -108,14 +108,14 @@ def base_options describe "#text_field" do describe "when called with a provided value" do before do - settings = double('Fake property', property => 'persisted value') + settings = double("Fake property", property => "persisted value") allow(object).to receive(property_set).and_return(settings) end it "build a text field with the provided value" do - expected_options = base_options.merge(:value => 'provided value') + expected_options = base_options.merge(value: "provided value") expect(template).to receive(:text_field).with(object_name, property, expected_options) - proxy.text_field(property, {:value => 'provided value'}) + proxy.text_field(property, {value: "provided value"}) end end end @@ -123,40 +123,36 @@ def base_options describe "#radio_button" do let(:expected_options) { base_options.merge( - id: "#{object_name}_#{property_set}_#{property}_hello", + id: "#{object_name}_#{property_set}_#{property}_hello", checked: false ) } - let(:faked_property) { double('Fake property', property => 'hello') } + let(:faked_property) { double("Fake property", property => "hello") } before do allow(object).to receive(property_set).and_return(faked_property) end it "generate a unique id when one is not provided" do - expected_options.merge!( - :id => "#{object_name}_#{property_set}_#{property}_pancake" - ) - expect(template).to receive(:radio_button).with(object_name, property, 'pancake', expected_options) - proxy.radio_button(property, 'pancake') + expected_options[:id] = "#{object_name}_#{property_set}_#{property}_pancake" + expect(template).to receive(:radio_button).with(object_name, property, "pancake", expected_options) + proxy.radio_button(property, "pancake") end describe "when called with checked true for a truth value" do it "call with checked true for a truth value" do - expected_options.merge!(:checked => true) - expect(template).to receive(:radio_button).with(object_name, property, 'hello', expected_options) - proxy.radio_button(property, 'hello') + expected_options[:checked] = true + expect(template).to receive(:radio_button).with(object_name, property, "hello", expected_options) + proxy.radio_button(property, "hello") end end describe "when called with a value of a different type" do - let(:faked_property) { double('Fake property', property => '1') } + let(:faked_property) { double("Fake property", property => "1") } it "call with checked false" do - expected_options.merge!( - :id => "#{object_name}_#{property_set}_#{property}_1" - ) + expected_options[:id] = "#{object_name}_#{property_set}_#{property}_1" expect(template).to receive(:radio_button).with(object_name, property, 1, expected_options) proxy.radio_button(property, 1) end @@ -165,12 +161,12 @@ def base_options describe "#select" do before do - settings = double('Fake property', :count => '2') + settings = double("Fake property", count: "2") allow(object).to receive(property_set).and_return(settings) end it "render a