From ca05877ebf70f4ccefb3787aa1c7da65d7abcca1 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Fri, 15 Sep 2023 13:59:53 +0200 Subject: [PATCH 1/5] Set up the Standard Ruby linter Run the linter with either - `bundle exec rake standard`, or - `bundle exec standardrb` --- .github/workflows/ci.yml | 11 ++++ .standard_todo.yml | 115 +++++++++++++++++++++++++++++++++++++++ Rakefile | 3 +- property_sets.gemspec | 1 + 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 .standard_todo.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4fdad17..20acb2c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,3 +52,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/.standard_todo.yml b/.standard_todo.yml new file mode 100644 index 0000000..1237432 --- /dev/null +++ b/.standard_todo.yml @@ -0,0 +1,115 @@ +# Auto generated files with errors to ignore. +# Remove from this list as you refactor files. +--- +ignore: +- Rakefile: + - Style/StringLiterals +- benchmark/read.rb: + - Style/StringLiterals + - Style/HashSyntax + - Layout/ExtraSpacing + - Layout/SpaceInsideHashLiteralBraces + - Layout/EmptyLinesAroundClassBody + - Layout/EmptyLinesAroundBlockBody +- gemfiles/rails6.0.gemfile: + - Style/StringLiterals +- gemfiles/rails6.1.gemfile: + - Style/StringLiterals +- gemfiles/rails7.0.gemfile: + - Style/StringLiterals +- lib/property_sets.rb: + - Style/StringLiterals + - Layout/ExtraSpacing +- lib/property_sets/action_view_extension.rb: + - Style/StringLiterals + - Layout/ExtraSpacing + - Style/RedundantInterpolation + - Performance/InefficientHashSearch + - Layout/SpaceInsideHashLiteralBraces + - Style/HashSyntax + - Layout/SpaceInsideParens + - Layout/EmptyLinesAroundClassBody +- lib/property_sets/active_record_extension.rb: + - Style/StringLiterals + - Style/RedundantSelf + - Performance/RegexpMatch + - Style/HashSyntax + - Style/TrailingCommaInHashLiteral + - Style/MultilineIfThen + - Layout/ExtraSpacing + - Layout/SpaceInsideArrayLiteralBrackets + - Style/RedundantInterpolation + - Performance/FlatMap + - Lint/AssignmentInCondition + - Style/Alias + - Layout/SpaceBeforeBlockBraces + - Layout/SpaceInsideBlockBraces + - Layout/SpaceAfterComma + - Layout/EmptyLinesAroundModuleBody +- lib/property_sets/casting.rb: + - Style/StringLiterals + - Layout/SpaceInsideArrayLiteralBrackets + - Lint/DeprecatedConstants +- lib/property_sets/delegator.rb: + - Style/RedundantSelf + - Layout/SpaceBeforeBlockBraces + - Layout/SpaceInsideBlockBraces + - Layout/SpaceAfterComma +- lib/property_sets/property_set_model.rb: + - Style/StringLiterals + - Layout/HashAlignment + - Style/TrailingCommaInHashLiteral + - Layout/SpaceInsideArrayLiteralBrackets + - Layout/ExtraSpacing + - Style/HashSyntax + - Layout/EmptyLinesAroundModuleBody +- property_sets.gemspec: + - Style/StringLiterals + - Layout/ExtraSpacing +- spec/casting_spec.rb: + - Style/StringLiterals + - Layout/SpaceAroundMethodCallOperator + - Layout/SpaceAfterComma + - Layout/ExtraSpacing +- spec/delegator_spec.rb: + - Style/StringLiterals + - Style/HashSyntax +- spec/inheritance_spec.rb: + - Style/StringLiterals + - Layout/RescueEnsureAlignment + - Layout/BeginEndAlignment +- spec/property_sets_spec.rb: + - Style/StringLiterals + - Style/HashSyntax + - Style/PercentLiteralDelimiters + - Lint/ConstantDefinitionInBlock + - Layout/ArgumentAlignment + - Layout/ExtraSpacing + - Layout/SpaceAroundMethodCallOperator + - Layout/SpaceInsideArrayLiteralBrackets + - Layout/SpaceInsideHashLiteralBraces + - Layout/EmptyLinesAroundBlockBody +- spec/spec_helper.rb: + - Style/StringLiterals +- spec/support/database_config.rb: + - Style/HashSyntax + - Layout/HashAlignment + - Style/TrailingCommaInHashLiteral +- spec/support/database_migrations.rb: + - Style/StringLiterals + - Style/GlobalStdStream + - Style/HashSyntax + - Layout/ExtraSpacing + - Layout/SpaceInsideArrayLiteralBrackets +- spec/support/models.rb: + - Layout/EmptyLineAfterMagicComment + - Style/StringLiterals + - Style/HashSyntax +- spec/view_extensions_spec.rb: + - Style/StringLiterals + - Layout/HashAlignment + - Style/HashSyntax + - Layout/ExtraSpacing + - Layout/IndentationWidth + - Performance/RedundantMerge + - Layout/SpaceInsideHashLiteralBraces diff --git a/Rakefile b/Rakefile index 9287292..172f1a3 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,9 @@ 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/property_sets.gemspec b/property_sets.gemspec index d135a7d..6df22b5 100644 --- a/property_sets.gemspec +++ b/property_sets.gemspec @@ -17,6 +17,7 @@ Gem::Specification.new "property_sets", PropertySets::VERSION do |s| s.add_development_dependency("rake") 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") From 0f442b1777212eb5f0574f1d66d8692d7b42c94d Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Fri, 15 Sep 2023 14:00:02 +0200 Subject: [PATCH 2/5] StandardRb: Fix Style/StringLiterals offenses --- .standard_todo.yml | 24 --------- Rakefile | 8 +-- benchmark/read.rb | 2 +- gemfiles/rails6.0.gemfile | 8 +-- gemfiles/rails6.1.gemfile | 8 +-- gemfiles/rails7.0.gemfile | 8 +-- lib/property_sets.rb | 8 +-- lib/property_sets/action_view_extension.rb | 6 +-- lib/property_sets/active_record_extension.rb | 6 +-- lib/property_sets/casting.rb | 2 +- lib/property_sets/property_set_model.rb | 20 +++---- property_sets.gemspec | 16 +++--- spec/casting_spec.rb | 4 +- spec/delegator_spec.rb | 16 +++--- spec/inheritance_spec.rb | 6 +-- spec/property_sets_spec.rb | 28 +++++----- spec/spec_helper.rb | 24 ++++----- spec/support/database_migrations.rb | 4 +- spec/support/models.rb | 6 +-- spec/view_extensions_spec.rb | 56 ++++++++++---------- 20 files changed, 118 insertions(+), 142 deletions(-) diff --git a/.standard_todo.yml b/.standard_todo.yml index 1237432..783fdfa 100644 --- a/.standard_todo.yml +++ b/.standard_todo.yml @@ -2,26 +2,15 @@ # Remove from this list as you refactor files. --- ignore: -- Rakefile: - - Style/StringLiterals - benchmark/read.rb: - - Style/StringLiterals - Style/HashSyntax - Layout/ExtraSpacing - Layout/SpaceInsideHashLiteralBraces - Layout/EmptyLinesAroundClassBody - Layout/EmptyLinesAroundBlockBody -- gemfiles/rails6.0.gemfile: - - Style/StringLiterals -- gemfiles/rails6.1.gemfile: - - Style/StringLiterals -- gemfiles/rails7.0.gemfile: - - Style/StringLiterals - lib/property_sets.rb: - - Style/StringLiterals - Layout/ExtraSpacing - lib/property_sets/action_view_extension.rb: - - Style/StringLiterals - Layout/ExtraSpacing - Style/RedundantInterpolation - Performance/InefficientHashSearch @@ -30,7 +19,6 @@ ignore: - Layout/SpaceInsideParens - Layout/EmptyLinesAroundClassBody - lib/property_sets/active_record_extension.rb: - - Style/StringLiterals - Style/RedundantSelf - Performance/RegexpMatch - Style/HashSyntax @@ -47,7 +35,6 @@ ignore: - Layout/SpaceAfterComma - Layout/EmptyLinesAroundModuleBody - lib/property_sets/casting.rb: - - Style/StringLiterals - Layout/SpaceInsideArrayLiteralBrackets - Lint/DeprecatedConstants - lib/property_sets/delegator.rb: @@ -56,7 +43,6 @@ ignore: - Layout/SpaceInsideBlockBraces - Layout/SpaceAfterComma - lib/property_sets/property_set_model.rb: - - Style/StringLiterals - Layout/HashAlignment - Style/TrailingCommaInHashLiteral - Layout/SpaceInsideArrayLiteralBrackets @@ -64,22 +50,17 @@ ignore: - Style/HashSyntax - Layout/EmptyLinesAroundModuleBody - property_sets.gemspec: - - Style/StringLiterals - Layout/ExtraSpacing - spec/casting_spec.rb: - - Style/StringLiterals - Layout/SpaceAroundMethodCallOperator - Layout/SpaceAfterComma - Layout/ExtraSpacing - spec/delegator_spec.rb: - - Style/StringLiterals - Style/HashSyntax - spec/inheritance_spec.rb: - - Style/StringLiterals - Layout/RescueEnsureAlignment - Layout/BeginEndAlignment - spec/property_sets_spec.rb: - - Style/StringLiterals - Style/HashSyntax - Style/PercentLiteralDelimiters - Lint/ConstantDefinitionInBlock @@ -89,24 +70,19 @@ ignore: - Layout/SpaceInsideArrayLiteralBrackets - Layout/SpaceInsideHashLiteralBraces - Layout/EmptyLinesAroundBlockBody -- spec/spec_helper.rb: - - Style/StringLiterals - spec/support/database_config.rb: - Style/HashSyntax - Layout/HashAlignment - Style/TrailingCommaInHashLiteral - spec/support/database_migrations.rb: - - Style/StringLiterals - Style/GlobalStdStream - Style/HashSyntax - Layout/ExtraSpacing - Layout/SpaceInsideArrayLiteralBrackets - spec/support/models.rb: - Layout/EmptyLineAfterMagicComment - - Style/StringLiterals - Style/HashSyntax - spec/view_extensions_spec.rb: - - Style/StringLiterals - Layout/HashAlignment - Style/HashSyntax - Layout/ExtraSpacing diff --git a/Rakefile b/Rakefile index 172f1a3..15c219a 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ -require 'bundler/gem_tasks' -require 'bump/tasks' -require 'rspec/core/rake_task' -require 'standard/rake' +require "bundler/gem_tasks" +require "bump/tasks" +require "rspec/core/rake_task" +require "standard/rake" task default: [:spec, :standard] diff --git a/benchmark/read.rb b/benchmark/read.rb index f60ea43..d901076 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: 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..077ec6a 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 diff --git a/lib/property_sets/action_view_extension.rb b/lib/property_sets/action_view_extension.rb index e3eed1f..08b3220 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 @@ -75,8 +75,8 @@ 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 diff --git a/lib/property_sets/active_record_extension.rb b/lib/property_sets/active_record_extension.rb index e77fe41..15c707e 100644 --- a/lib/property_sets/active_record_extension.rb +++ b/lib/property_sets/active_record_extension.rb @@ -1,6 +1,6 @@ -require 'active_record' -require 'property_sets/casting' -require 'set' +require "active_record" +require "property_sets/casting" +require "set" module PropertySets module ActiveRecordExtension diff --git a/lib/property_sets/casting.rb b/lib/property_sets/casting.rb index 7054a15..6be1d6b 100644 --- a/lib/property_sets/casting.rb +++ b/lib/property_sets/casting.rb @@ -1,4 +1,4 @@ -require 'json' +require "json" module PropertySets module Casting diff --git a/lib/property_sets/property_set_model.rb b/lib/property_sets/property_set_model.rb index ac7fe0a..c5a949f 100644 --- a/lib/property_sets/property_set_model.rb +++ b/lib/property_sets/property_set_model.rb @@ -1,17 +1,17 @@ -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 @@ -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) diff --git a/property_sets.gemspec b/property_sets.gemspec index 6df22b5..da5abc3 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.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.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,10 +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('standard') - 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..7c2ab9a 100644 --- a/spec/casting_spec.rb +++ b/spec/casting_spec.rb @@ -1,5 +1,5 @@ -require 'spec_helper' -require 'property_sets/casting' +require "spec_helper" +require "property_sets/casting" describe PropertySets::Casting do describe "#read" do diff --git a/spec/delegator_spec.rb b/spec/delegator_spec.rb index 1096da4..1ad0048 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(: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..9b4391e 100644 --- a/spec/inheritance_spec.rb +++ b/spec/inheritance_spec.rb @@ -1,6 +1,6 @@ -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 diff --git a/spec/property_sets_spec.rb b/spec/property_sets_spec.rb index 6887251..5472f89 100644 --- a/spec/property_sets_spec.rb +++ b/spec/property_sets_spec.rb @@ -1,9 +1,9 @@ -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 @@ -33,7 +33,7 @@ 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 @@ -88,7 +88,7 @@ class AnotherThing < MainDatabase 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.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 @@ -96,22 +96,22 @@ class AnotherThing < MainDatabase 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 @@ -415,13 +415,13 @@ 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 @@ -483,7 +483,7 @@ class AnotherThing < MainDatabase 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 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_migrations.rb b/spec/support/database_migrations.rb index edd4fbe..1cb7102 100644 --- a/spec/support/database_migrations.rb +++ b/spec/support/database_migrations.rb @@ -1,10 +1,10 @@ # setup database -require 'active_record' +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 diff --git a/spec/support/models.rb b/spec/support/models.rb index 64c07b9..9f81728 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -require_relative 'acts_like_an_integer' +require_relative "acts_like_an_integer" if LEGACY_CONNECTION_HANDLING class MainDatabase < ActiveRecord::Base @@ -38,7 +38,7 @@ module Woot # doesn't actually seem to be used in AR4 ? property :foo property :bar property :baz - property :hep, :default => 'skep' + property :hep, :default => "skep" property :pro, :protected => true property :bool_true, :type => :boolean, :default => true property :bool_false, :type => :boolean, :default => false @@ -101,7 +101,7 @@ module Woot # doesn't actually seem to be used in AR4 ? property :foo property :bar property :baz - property :hep, :default => 'skep' + property :hep, :default => "skep" property :pro, :protected => true property :bool_true, :type => :boolean, :default => true property :bool_false, :type => :boolean, :default => false diff --git a/spec/view_extensions_spec.rb b/spec/view_extensions_spec.rb index bd77f82..d400d5c 100644 --- a/spec/view_extensions_spec.rb +++ b/spec/view_extensions_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe "property set view extensions" do def base_options @@ -11,9 +11,9 @@ def base_options let(:property_set) { :settings } let(:property) { :active } - let(:object_name) { 'object_name' } - let(:object) { double('View object') } - let(:template) { double('Template') } + 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) } @@ -22,11 +22,11 @@ def base_options 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') + 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') + 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 @@ -128,7 +128,7 @@ def base_options ) } - 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) @@ -138,20 +138,20 @@ def base_options 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') + 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') + 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!( @@ -165,7 +165,7 @@ 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 From 9f9fbc7e8e711fd3c3f1377c415dffba14d08d48 Mon Sep 17 00:00:00 2001 From: Benjamin Quorning Date: Fri, 15 Sep 2023 14:03:54 +0200 Subject: [PATCH 3/5] StandardRb: Fix Layout offenses --- .standard_todo.yml | 47 --------- benchmark/read.rb | 15 +-- lib/property_sets.rb | 2 +- lib/property_sets/action_view_extension.rb | 18 ++-- lib/property_sets/active_record_extension.rb | 9 +- lib/property_sets/casting.rb | 2 +- lib/property_sets/delegator.rb | 2 +- lib/property_sets/property_set_model.rb | 27 +++-- property_sets.gemspec | 8 +- spec/casting_spec.rb | 28 ++--- spec/inheritance_spec.rb | 8 +- spec/property_sets_spec.rb | 49 +++++---- spec/support/database_config.rb | 2 +- spec/support/database_migrations.rb | 102 +++++++++---------- spec/support/models.rb | 1 + spec/view_extensions_spec.rb | 34 +++---- 16 files changed, 149 insertions(+), 205 deletions(-) diff --git a/.standard_todo.yml b/.standard_todo.yml index 783fdfa..61b522f 100644 --- a/.standard_todo.yml +++ b/.standard_todo.yml @@ -4,88 +4,41 @@ ignore: - benchmark/read.rb: - Style/HashSyntax - - Layout/ExtraSpacing - - Layout/SpaceInsideHashLiteralBraces - - Layout/EmptyLinesAroundClassBody - - Layout/EmptyLinesAroundBlockBody -- lib/property_sets.rb: - - Layout/ExtraSpacing - lib/property_sets/action_view_extension.rb: - - Layout/ExtraSpacing - Style/RedundantInterpolation - Performance/InefficientHashSearch - - Layout/SpaceInsideHashLiteralBraces - Style/HashSyntax - - Layout/SpaceInsideParens - - Layout/EmptyLinesAroundClassBody - lib/property_sets/active_record_extension.rb: - Style/RedundantSelf - Performance/RegexpMatch - Style/HashSyntax - Style/TrailingCommaInHashLiteral - Style/MultilineIfThen - - Layout/ExtraSpacing - - Layout/SpaceInsideArrayLiteralBrackets - Style/RedundantInterpolation - Performance/FlatMap - Lint/AssignmentInCondition - Style/Alias - - Layout/SpaceBeforeBlockBraces - - Layout/SpaceInsideBlockBraces - - Layout/SpaceAfterComma - - Layout/EmptyLinesAroundModuleBody - lib/property_sets/casting.rb: - - Layout/SpaceInsideArrayLiteralBrackets - Lint/DeprecatedConstants - lib/property_sets/delegator.rb: - Style/RedundantSelf - - Layout/SpaceBeforeBlockBraces - - Layout/SpaceInsideBlockBraces - - Layout/SpaceAfterComma - lib/property_sets/property_set_model.rb: - - Layout/HashAlignment - Style/TrailingCommaInHashLiteral - - Layout/SpaceInsideArrayLiteralBrackets - - Layout/ExtraSpacing - Style/HashSyntax - - Layout/EmptyLinesAroundModuleBody -- property_sets.gemspec: - - Layout/ExtraSpacing -- spec/casting_spec.rb: - - Layout/SpaceAroundMethodCallOperator - - Layout/SpaceAfterComma - - Layout/ExtraSpacing - spec/delegator_spec.rb: - Style/HashSyntax -- spec/inheritance_spec.rb: - - Layout/RescueEnsureAlignment - - Layout/BeginEndAlignment - spec/property_sets_spec.rb: - Style/HashSyntax - Style/PercentLiteralDelimiters - Lint/ConstantDefinitionInBlock - - Layout/ArgumentAlignment - - Layout/ExtraSpacing - - Layout/SpaceAroundMethodCallOperator - - Layout/SpaceInsideArrayLiteralBrackets - - Layout/SpaceInsideHashLiteralBraces - - Layout/EmptyLinesAroundBlockBody - spec/support/database_config.rb: - Style/HashSyntax - - Layout/HashAlignment - Style/TrailingCommaInHashLiteral - spec/support/database_migrations.rb: - Style/GlobalStdStream - Style/HashSyntax - - Layout/ExtraSpacing - - Layout/SpaceInsideArrayLiteralBrackets - spec/support/models.rb: - - Layout/EmptyLineAfterMagicComment - Style/HashSyntax - spec/view_extensions_spec.rb: - - Layout/HashAlignment - Style/HashSyntax - - Layout/ExtraSpacing - - Layout/IndentationWidth - Performance/RedundantMerge - - Layout/SpaceInsideHashLiteralBraces diff --git a/benchmark/read.rb b/benchmark/read.rb index d901076..7d0418e 100644 --- a/benchmark/read.rb +++ b/benchmark/read.rb @@ -9,15 +9,15 @@ 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 @@ -25,13 +25,10 @@ class Account < ActiveRecord::Base 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") end @@ -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/lib/property_sets.rb b/lib/property_sets.rb index 077ec6a..e6613ee 100644 --- a/lib/property_sets.rb +++ b/lib/property_sets.rb @@ -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 08b3220..245cd58 100644 --- a/lib/property_sets/action_view_extension.rb +++ b/lib/property_sets/action_view_extension.rb @@ -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") @@ -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 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 15c707e..e4e716a 100644 --- a/lib/property_sets/active_record_extension.rb +++ b/lib/property_sets/active_record_extension.rb @@ -61,9 +61,9 @@ def property_set(association, options = {}, &block) # 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 @@ -165,7 +165,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) @@ -214,7 +214,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 +241,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 6be1d6b..c4791fc 100644 --- a/lib/property_sets/casting.rb +++ b/lib/property_sets/casting.rb @@ -2,7 +2,7 @@ module PropertySets module Casting - FALSE = [ "false", "0", "", "off", "n" ] + FALSE = ["false", "0", "", "off", "n"] class << self def read(type, value) diff --git a/lib/property_sets/delegator.rb b/lib/property_sets/delegator.rb index 67dcf25..3b6a633 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) } + self.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 c5a949f..a57697f 100644 --- a/lib/property_sets/property_set_model.rb +++ b/lib/property_sets/property_set_model.rb @@ -4,19 +4,19 @@ 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, + "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, + "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? @@ -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 + 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) + 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 da5abc3..1c89b5c 100644 --- a/property_sets.gemspec +++ b/property_sets.gemspec @@ -1,12 +1,12 @@ 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.authors = ["Morten Primdahl"] + s.email = "primdahl@me.com" s.homepage = "http://github.com/zendesk/property_sets" - s.license = "Apache License Version 2.0" + s.license = "Apache License Version 2.0" s.required_ruby_version = ">= 2.7" diff --git a/spec/casting_spec.rb b/spec/casting_spec.rb index 7c2ab9a..fab4d3c 100644 --- a/spec/casting_spec.rb +++ b/spec/casting_spec.rb @@ -5,27 +5,27 @@ 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/inheritance_spec.rb b/spec/inheritance_spec.rb index 9b4391e..d1da522 100644 --- a/spec/inheritance_spec.rb +++ b/spec/inheritance_spec.rb @@ -4,10 +4,10 @@ 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 5472f89..650f5b3 100644 --- a/spec/property_sets_spec.rb +++ b/spec/property_sets_spec.rb @@ -45,7 +45,7 @@ class AnotherThing < MainDatabase end AnotherThing.property_set(:settings, :extend => Parent::Account::Woot, - :table_name => "thing_settings") + :table_name => "thing_settings") expect(AnotherThing.new.settings.extensions).to include(::Parent::Account::Woot) end @@ -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,18 +85,18 @@ 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") end @@ -122,7 +122,7 @@ 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] + valids = %w(hello hel_lo hell0) + [:hello] invalids = %w(_hello) valids.each do |valid| @@ -145,9 +145,9 @@ class AnotherThing < MainDatabase 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 @@ -174,7 +174,7 @@ class AnotherThing < MainDatabase 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) + record = account.settings.lookup(:baz) expect(record).to be_new_record expect(record.account).to eq(account) @@ -244,25 +244,25 @@ class AnotherThing < MainDatabase end it "work identically for new and existing owner objects" do - [ account, Parent::Account.new(:name => "Mibble") ].each do |account| + [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") 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! @@ -271,8 +271,8 @@ class AnotherThing < MainDatabase 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" } + {: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") @@ -290,7 +290,7 @@ class AnotherThing < MainDatabase account.update_attributes!( :name => "Kim", - :settings => { :foo => "1", :baz => "0", :pro => "1" } + :settings => {:foo => "1", :baz => "0", :pro => "1"} ) account.reload @@ -426,7 +426,6 @@ class AnotherThing < MainDatabase end describe "typed columns" do - it "typecast the default value" do expect(account.typed_data.association_class.default(:default_prop)).to eq(123) end diff --git a/spec/support/database_config.rb b/spec/support/database_config.rb index 4d7f2ca..088a468 100644 --- a/spec/support/database_config.rb +++ b/spec/support/database_config.rb @@ -3,7 +3,7 @@ config = { :test => { :test_database => { - :adapter => "sqlite3", + :adapter => "sqlite3", :database => ":memory:", }, :test_alt_database => { diff --git a/spec/support/database_migrations.rb b/spec/support/database_migrations.rb index 1cb7102..65d2ebe 100644 --- a/spec/support/database_migrations.rb +++ b/spec/support/database_migrations.rb @@ -15,92 +15,92 @@ def connection def change create_table "account_benchmark_settings", :force => true do |t| - t.integer "account_id" - t.string "name" - t.string "value" + 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" + t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "account_alt_dbs", :force => true do |t| - t.string "name" + 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" + 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" + 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" + 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" + 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) + 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" + 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" + 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 @@ -113,54 +113,54 @@ def connection def change create_table "account_alt_db_settings", :force => true do |t| - t.integer "account_alt_db_id" - t.string "name" - t.string "value" + 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" + 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" + 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" + 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) + 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 9f81728..d5eebf7 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require_relative "acts_like_an_integer" if LEGACY_CONNECTION_HANDLING diff --git a/spec/view_extensions_spec.rb b/spec/view_extensions_spec.rb index d400d5c..ff40bb3 100644 --- a/spec/view_extensions_spec.rb +++ b/spec/view_extensions_spec.rb @@ -3,19 +3,19 @@ describe "property set view extensions" do def base_options { - :name => "#{object_name}[#{property_set}][#{property}]", - :id => "#{object_name}_#{property_set}_#{property}", + :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) @@ -123,7 +123,7 @@ 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 ) } @@ -135,11 +135,11 @@ def base_options 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.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") end describe "when called with checked true for a truth value" do @@ -170,7 +170,7 @@ def base_options end it "render a with