diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 15ef1e5..34522b6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7'] + ruby-version: ['3.4', '3.3', '3.2'] steps: - uses: actions/checkout@v4 @@ -25,8 +25,8 @@ jobs: bundle install bundle exec appraisal install RAILS_ENV=test bundle exec rake dummy:db:reset - - name: Rubocop - run: rubocop + - name: Standard + run: bundle exec standardrb - name: Run tests run: | RAILS_ENV=test bundle exec rake dummy:db:schema:load diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index 1df2325..0000000 --- a/.rubocop.yml +++ /dev/null @@ -1,66 +0,0 @@ -AllCops: - NewCops: enable - SuggestExtensions: false - TargetRubyVersion: 3.1 - Include: - - 'lib/**/*.rb' - - 'test/**/*.rb' - - 'spec/**/*.rb' - - '**/Gemfile' - - '**/Rakefile' - Exclude: - - 'bin/**/*' - - 'spec/fixtures/**/*.rb' - - 'spec/dummy/**/*.rb' - - 'lib/generators/**/templates/**' - -Style/HashSyntax: - EnforcedShorthandSyntax: never - -Style/Documentation: - Enabled: false - -Naming/BlockForwarding: - Enabled: false - -Style/RedundantSelf: - Enabled: false - -Style/RedundantReturn: - Enabled: false - -Style/GuardClause: - Enabled: false - -Style/ClassAndModuleChildren: - Enabled: false - -Layout/EmptyLinesAroundClassBody: - Enabled: false - -Style/FrozenStringLiteralComment: - Enabled: false - -Layout/CommentIndentation: - Enabled: false - -Layout/LineLength: - Max: 120 - -Metrics/ClassLength: - Max: 120 - -Layout/EmptyLineBetweenDefs: - AllowAdjacentOneLineDefs: true - -Naming/MethodParameterName: - AllowedNames: - - _ - -Style/ClassVars: - Exclude: - - 'lib/slayer/service.rb' - -Style/MutableConstant: - Exclude: - - 'lib/slayer/version.rb' diff --git a/.standard.yml b/.standard.yml new file mode 100644 index 0000000..b0b2f13 --- /dev/null +++ b/.standard.yml @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Standard Ruby configuration +# https://github.com/testdouble/standard + +ruby_version: 3.2 + +ignore: + - 'bin/**/*' + - 'vendor/**/*' + - 'tmp/**/*' + - 'lib/generators/**/templates/**/*' + +fix: true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 8c33775..0000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -sudo: false -language: ruby -cache: bundler -before_install: - - gem update --system - - gem update bundler - - gem cleanup bundler -matrix: - include: - - rvm: 2.4.0 - gemfile: gemfiles/rails_5.0.gemfile - - rvm: 2.3.0 - gemfile: gemfiles/rails_4.2.gemfile - - rvm: 2.3.0 - gemfile: gemfiles/rails_5.0.gemfile diff --git a/Appraisals b/Appraisals index 8c32075..c2be193 100644 --- a/Appraisals +++ b/Appraisals @@ -1,16 +1,25 @@ # frozen_string_literal: true -appraise 'rails_6.1' do - gem 'rails', '~> 6.1' - gem 'tzinfo-data' +appraise "rails_7.0" do + gem "rails", "~> 7.0" + gem "rspec", "~> 3.12" + gem "rspec-rails", "~> 6.0" + gem "rails-controller-testing" + gem "sqlite3", "~> 1.4" end -appraise 'rails_7.0' do - gem 'rails', '~> 7.0' - gem 'tzinfo-data' +appraise "rails_7.1" do + gem "rails", "~> 7.1" + gem "rspec", "~> 3.12" + gem "rspec-rails", "~> 6.1" + gem "rails-controller-testing" + gem "sqlite3", "~> 1.4" end -appraise 'rails_7.1' do - gem 'rails', '~> 7.1' - gem 'tzinfo-data' +appraise "rails_8.0" do + gem "rails", "~> 8.0" + gem "rspec", "~> 3.13" + gem "rspec-rails", "~> 8.0" + gem "rails-controller-testing" + gem "sqlite3", "~> 2.1" end diff --git a/Gemfile b/Gemfile index e7ca831..5c3e5d5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,13 +1,13 @@ # frozen_string_literal: true -source 'https://rubygems.org' +source "https://rubygems.org" # Specify your gem's dependencies in slayer_rails.gemspec gemspec -gem 'appraisal' -gem 'byebug' -gem 'nokogiri' -gem 'rspec-rails' -gem 'sqlite3' -gem 'tzinfo-data' +gem "appraisal" +gem "byebug" +gem "nokogiri" +gem "rspec-rails" +gem "sqlite3" +gem "tzinfo-data" diff --git a/Rakefile b/Rakefile index 2e3bb9d..f113155 100644 --- a/Rakefile +++ b/Rakefile @@ -1,14 +1,14 @@ # frozen_string_literal: true -require 'rubygems' -require 'bundler/setup' -require 'bundler/gem_tasks' +require "rubygems" +require "bundler/setup" +require "bundler/gem_tasks" -require 'rake' -require 'rspec/core/rake_task' +require "rake" +require "rspec/core/rake_task" namespace :dummy do - require_relative 'spec/dummy/application' + require_relative "spec/dummy/application" Dummy::Application.load_tasks end diff --git a/config.ru b/config.ru index af62932..776beb0 100644 --- a/config.ru +++ b/config.ru @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'rubygems' -require 'bundler' +require "rubygems" +require "bundler" Bundler.require :default, :development diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile index a6cdcec..ea05aed 100644 --- a/gemfiles/rails_7.0.gemfile +++ b/gemfiles/rails_7.0.gemfile @@ -5,9 +5,11 @@ source "https://rubygems.org" gem "appraisal" gem "byebug" gem "nokogiri" -gem "rspec-rails" -gem "sqlite3" +gem "rspec-rails", "~> 6.0" +gem "sqlite3", "~> 1.4" gem "tzinfo-data" gem "rails", "~> 7.0" +gem "rspec", "~> 3.12" +gem "rails-controller-testing" gemspec path: "../" diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile index d12ce35..6f8eb95 100644 --- a/gemfiles/rails_7.1.gemfile +++ b/gemfiles/rails_7.1.gemfile @@ -5,9 +5,11 @@ source "https://rubygems.org" gem "appraisal" gem "byebug" gem "nokogiri" -gem "rspec-rails" -gem "sqlite3" +gem "rspec-rails", "~> 6.1" +gem "sqlite3", "~> 1.4" gem "tzinfo-data" gem "rails", "~> 7.1" +gem "rspec", "~> 3.12" +gem "rails-controller-testing" gemspec path: "../" diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_8.0.gemfile similarity index 54% rename from gemfiles/rails_6.1.gemfile rename to gemfiles/rails_8.0.gemfile index a55d47c..0b796d6 100644 --- a/gemfiles/rails_6.1.gemfile +++ b/gemfiles/rails_8.0.gemfile @@ -5,9 +5,11 @@ source "https://rubygems.org" gem "appraisal" gem "byebug" gem "nokogiri" -gem "rspec-rails" -gem "sqlite3" +gem "rspec-rails", "~> 8.0" +gem "sqlite3", "~> 2.1" gem "tzinfo-data" -gem "rails", "~> 6.1" +gem "rails", "~> 8.0" +gem "rspec", "~> 3.13" +gem "rails-controller-testing" gemspec path: "../" diff --git a/lib/generators/command/command_generator.rb b/lib/generators/command/command_generator.rb index b59c66c..555449b 100644 --- a/lib/generators/command/command_generator.rb +++ b/lib/generators/command/command_generator.rb @@ -3,13 +3,13 @@ module Slayer module Generators class CommandGenerator < NamedBase - desc 'This generator creates new Slayer::Commands' + desc "This generator creates new Slayer::Commands" - source_root File.expand_path('templates', __dir__) - check_class_collision suffix: 'Command' + source_root File.expand_path("templates", __dir__) + check_class_collision suffix: "Command" def create_command_files - template 'command.rb', File.join('app', 'commands', class_path, "#{file_name}_command.rb") + template "command.rb", File.join("app", "commands", class_path, "#{file_name}_command.rb") end end end diff --git a/lib/generators/form/form_generator.rb b/lib/generators/form/form_generator.rb index a7dec1d..e8e667b 100644 --- a/lib/generators/form/form_generator.rb +++ b/lib/generators/form/form_generator.rb @@ -3,23 +3,23 @@ module Slayer module Generators class FormGenerator < NamedBase - desc 'This generator creates new Slayer::Forms' + desc "This generator creates new Slayer::Forms" - source_root File.expand_path('templates', __dir__) - check_class_collision suffix: 'Form' + source_root File.expand_path("templates", __dir__) + check_class_collision suffix: "Form" argument :name, type: :string argument :fields, type: :array, required: false, - desc: 'The attributes of the generated form. name:String completed:Boolean' + desc: "The attributes of the generated form. name:String completed:Boolean" def initialize(args, *options) # :nodoc: super args.shift - @fields = args.map { |a| a.split(':') } + @fields = args.map { |a| a.split(":") } end def create_form_files - template 'form.rb', File.join('app', 'forms', class_path, "#{file_name}_form.rb") + template "form.rb", File.join("app", "forms", class_path, "#{file_name}_form.rb") end end end diff --git a/lib/generators/scaffold/scaffold_generator.rb b/lib/generators/scaffold/scaffold_generator.rb index 22ac229..00d52b0 100644 --- a/lib/generators/scaffold/scaffold_generator.rb +++ b/lib/generators/scaffold/scaffold_generator.rb @@ -3,14 +3,14 @@ module Slayer module Generators class ScaffoldGenerator < NamedBase - desc 'This generator creates a new Slayer::Commands and a corresponding Slayer::Form' + desc "This generator creates a new Slayer::Commands and a corresponding Slayer::Form" - source_root File.expand_path('templates', __dir__) - check_class_collision suffix: 'Command' + source_root File.expand_path("templates", __dir__) + check_class_collision suffix: "Command" argument :name, type: :string argument :fields, type: :array, required: false, - desc: 'The attributes of the generated form. name:String completed:Boolean' + desc: "The attributes of the generated form. name:String completed:Boolean" def initialize(args, *options) # :nodoc: super @@ -18,11 +18,11 @@ def initialize(args, *options) # :nodoc: end def create_command_files - generate 'slayer:command', file_name.to_s + generate "slayer:command", file_name.to_s end def create_form_files - generate 'slayer:form', @args.join(' ').to_s + generate "slayer:form", @args.join(" ").to_s end end end diff --git a/lib/generators/slayer_base.rb b/lib/generators/slayer_base.rb index b19911a..08f00a2 100644 --- a/lib/generators/slayer_base.rb +++ b/lib/generators/slayer_base.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'rails/generators' -require 'rails/generators/named_base' +require "rails/generators" +require "rails/generators/named_base" module Slayer module Generators diff --git a/lib/slayer_rails.rb b/lib/slayer_rails.rb index 590073d..c0e613c 100644 --- a/lib/slayer_rails.rb +++ b/lib/slayer_rails.rb @@ -1,19 +1,19 @@ # frozen_string_literal: true -require 'active_support' -require 'active_model' -require 'active_record' -require 'slayer' +require "active_support" +require "active_model" +require "active_record" +require "slayer" -require 'slayer_rails/version' -require 'slayer_rails/extensions/form' -require 'slayer_rails/extensions/transaction' -require 'slayer_rails/extensions/translation' +require "slayer_rails/version" +require "slayer_rails/extensions/form" +require "slayer_rails/extensions/transaction" +require "slayer_rails/extensions/translation" -require 'generators/slayer_base' -require 'generators/command/command_generator' -require 'generators/form/form_generator' -require 'generators/scaffold/scaffold_generator' +require "generators/slayer_base" +require "generators/command/command_generator" +require "generators/form/form_generator" +require "generators/scaffold/scaffold_generator" module Slayer class Form diff --git a/lib/slayer_rails/extensions/form.rb b/lib/slayer_rails/extensions/form.rb index c4330dd..b66abce 100644 --- a/lib/slayer_rails/extensions/form.rb +++ b/lib/slayer_rails/extensions/form.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'json' +require "json" module SlayerRails module Extensions @@ -14,7 +14,7 @@ module Form def validate! return if valid? - message = errors.full_messages.join(', ') + message = errors.full_messages.join(", ") raise Slayer::FormValidationError, message unless valid? end @@ -30,23 +30,23 @@ def as_model(klass, attr_map = nil) class << self def from_params(params, additional_params: {}, root_key: nil) - params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params.to_h - params = params.deep_symbolize_keys + params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params.to_h + params = params.deep_symbolize_keys attr_names = attribute_set.map(&:name) params = params.fetch(root_key, {}) if root_key.present? attr_hash = params - .merge(params.slice(*attr_names)) - .merge(additional_params) + .merge(params.slice(*attr_names)) + .merge(additional_params) new(attr_hash) end def from_model(model) attr_hash = attribute_set.map(&:name) - .select { |attr_name| model.respond_to?(attr_name) } - .map { |attr_name| [attr_name, model.public_send(attr_name)] } + .select { |attr_name| model.respond_to?(attr_name) } + .map { |attr_name| [attr_name, model.public_send(attr_name)] } new(attr_hash.to_h) end diff --git a/lib/slayer_rails/extensions/translation.rb b/lib/slayer_rails/extensions/translation.rb index 6041346..149b9f0 100644 --- a/lib/slayer_rails/extensions/translation.rb +++ b/lib/slayer_rails/extensions/translation.rb @@ -19,7 +19,7 @@ def localize(*args, **kwargs) class << self def full_key(key) - return key unless key.start_with? '.' + return key unless key.start_with? "." "#{implied_path}#{key}" end @@ -29,16 +29,16 @@ def implied_path end def build_implied_path - module_path = name.underscore.split('/') + module_path = name.underscore.split("/") - class_name_parts = module_path.pop.split('_') + class_name_parts = module_path.pop.split("_") class_name_parts.pop if %w[command service form].include? class_name_parts.last module_path.unshift(slayer_type) - module_path.push(class_name_parts.join('_')) + module_path.push(class_name_parts.join("_")) - module_path.join('.') + module_path.join(".") end def slayer_type @@ -46,8 +46,8 @@ def slayer_type end def detect_slayer_type - return 'commands' if self <= Slayer::Command - return 'forms' if self <= Slayer::Form + return "commands" if self <= Slayer::Command + return "forms" if self <= Slayer::Form raise NotImplementedException, "Unknown Slayer Class: #{name}" end diff --git a/lib/slayer_rails/version.rb b/lib/slayer_rails/version.rb index 16550b5..eaacf71 100644 --- a/lib/slayer_rails/version.rb +++ b/lib/slayer_rails/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module SlayerRails - VERSION = '0.5.4' + VERSION = "0.5.4" end diff --git a/slayer_rails.gemspec b/slayer_rails.gemspec index f532f7f..9894e70 100644 --- a/slayer_rails.gemspec +++ b/slayer_rails.gemspec @@ -1,31 +1,34 @@ # frozen_string_literal: true -lib = File.expand_path('lib', __dir__) +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'slayer_rails/version' +require "slayer_rails/version" Gem::Specification.new do |spec| - spec.name = 'slayer_rails' - spec.version = SlayerRails::VERSION - spec.authors = ['Wyatt Kirby'] - spec.email = ['wyatt@apsis.io'] + spec.name = "slayer_rails" + spec.version = SlayerRails::VERSION + spec.authors = ["Wyatt Kirby"] + spec.email = ["wyatt@apsis.io"] - spec.summary = 'Rails extensions for Slayer.' - spec.homepage = 'http://www.apsis.io' - spec.license = 'MIT' + spec.summary = "Rails extensions for Slayer." + spec.homepage = "http://www.apsis.io" + spec.license = "MIT" - spec.files = `git ls-files -z`.split("\x0").reject do |f| + spec.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test|spec|features)/}) end - spec.bindir = 'exe' - spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ['lib'] + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] - spec.add_runtime_dependency 'rails', '>= 4.2.0' - spec.add_runtime_dependency 'slayer', '>= 0.5' + spec.required_ruby_version = ">= 3.2.0" - spec.add_development_dependency 'bundler', '~> 2.0' - spec.add_development_dependency 'rake', '~> 12.0' - spec.add_development_dependency 'rubocop', '= 1.38.0' + spec.add_dependency "rails", ">= 7.0.0" + spec.add_dependency "slayer", ">= 0.5" + + spec.add_development_dependency "bundler", "~> 2.4" + spec.add_development_dependency "rake", "~> 13.0" + spec.add_development_dependency "standard", "~> 1.39" + spec.metadata["rubygems_mfa_required"] = "true" end diff --git a/spec/dummy/application.rb b/spec/dummy/application.rb index 03e284a..d39a8b7 100644 --- a/spec/dummy/application.rb +++ b/spec/dummy/application.rb @@ -1,25 +1,25 @@ # frozen_string_literal: true -require 'rails/all' +require "rails/all" -require 'slayer_rails' +require "slayer_rails" module Dummy APP_ROOT = File.expand_path(__dir__).freeze class Application < Rails::Application config.action_controller.perform_caching = false - config.action_mailer.default_url_options = { host: 'dummy.example.com' } + config.action_mailer.default_url_options = {host: "dummy.example.com"} config.action_mailer.delivery_method = :test config.active_support.deprecation = :stderr config.eager_load = false - config.paths['app/controllers'] << "#{APP_ROOT}/app/controllers" - config.paths['app/models'] << "#{APP_ROOT}/app/models" - config.paths['app/views'] << "#{APP_ROOT}/app/views" - config.paths['config/database'] = "#{APP_ROOT}/config/database.yml" - config.paths['log'] = 'tmp/log/development.log' - config.paths.add 'config/routes.rb', with: "#{APP_ROOT}/config/routes.rb" + config.paths["app/controllers"] << "#{APP_ROOT}/app/controllers" + config.paths["app/models"] << "#{APP_ROOT}/app/models" + config.paths["app/views"] << "#{APP_ROOT}/app/views" + config.paths["config/database"] = "#{APP_ROOT}/config/database.yml" + config.paths["log"] = "tmp/log/development.log" + config.paths.add "config/routes.rb", with: "#{APP_ROOT}/config/routes.rb" def require_environment! initialize! diff --git a/spec/fixtures/commands/module_command.rb b/spec/fixtures/commands/module_command.rb index 76c428d..49aa321 100644 --- a/spec/fixtures/commands/module_command.rb +++ b/spec/fixtures/commands/module_command.rb @@ -3,7 +3,8 @@ module GoodCustomer module AwesomeProduct class RunFooBarCommand < Slayer::Command - def call; end + def call + end end end end diff --git a/spec/fixtures/commands/transaction_command.rb b/spec/fixtures/commands/transaction_command.rb index 276ce89..ab2de4b 100644 --- a/spec/fixtures/commands/transaction_command.rb +++ b/spec/fixtures/commands/transaction_command.rb @@ -7,7 +7,7 @@ def call end ok - rescue StandardError + rescue err end end diff --git a/spec/fixtures/forms/org_form.rb b/spec/fixtures/forms/org_form.rb index 15b2ab1..fb5152e 100644 --- a/spec/fixtures/forms/org_form.rb +++ b/spec/fixtures/forms/org_form.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative 'person_form' +require_relative "person_form" class OrgForm < Slayer::Form attribute :owner, ::PersonForm diff --git a/spec/lib/form_spec.rb b/spec/lib/form_spec.rb index a3132ab..5a0e3db 100644 --- a/spec/lib/form_spec.rb +++ b/spec/lib/form_spec.rb @@ -1,121 +1,121 @@ # frozen_string_literal: true -require 'spec_helper' -require 'json' +require "spec_helper" +require "json" # rubocop:disable Metrics/BlockLength describe Slayer::Form do - it 'is valid with good data' do - form = PersonForm.new({ name: 'Luke Skywalker', age: 20 }) + it "is valid with good data" do + form = PersonForm.new({name: "Luke Skywalker", age: 20}) - expect(form.name).to eq 'Luke Skywalker' + expect(form.name).to eq "Luke Skywalker" expect(form.age).to eq 20 expect(form.valid?).to be true expect(form.invalid?).to be false end - it 'is invalid with bad data' do - form = PersonForm.new({ name: 'Luke Skywalker', age: nil }) + it "is invalid with bad data" do + form = PersonForm.new({name: "Luke Skywalker", age: nil}) - expect(form.name).to eq 'Luke Skywalker' + expect(form.name).to eq "Luke Skywalker" expect(form.age).to eq nil expect(form.valid?).to be false expect(form.invalid?).to be true end - it 'raises exception if invalid' do + it "raises exception if invalid" do expect { PersonForm.new.validate! }.to raise_error(Slayer::FormValidationError) end - it 'instantiates from JSON' do - json = { name: 'Luke Skywalker', age: 20 }.to_json + it "instantiates from JSON" do + json = {name: "Luke Skywalker", age: 20}.to_json form = PersonForm.from_json(json) - expect(form.name).to eq 'Luke Skywalker' + expect(form.name).to eq "Luke Skywalker" expect(form.age).to eq 20 expect(form.valid?).to be true end - it 'instantiates from params' do - params = make_params({ name: 'Leia Organa', age: 20 }) - form = PersonForm.from_params(params) + it "instantiates from params" do + params = make_params({name: "Leia Organa", age: 20}) + form = PersonForm.from_params(params) - expect(form.name).to eq 'Leia Organa' + expect(form.name).to eq "Leia Organa" expect(form.age).to eq 20 expect(form.valid?).to be true end - it 'instantiates from params with root key' do - params = make_params({ person: { name: 'Leia Organa', age: 20 } }) - form = PersonForm.from_params(params, root_key: :person) + it "instantiates from params with root key" do + params = make_params({person: {name: "Leia Organa", age: 20}}) + form = PersonForm.from_params(params, root_key: :person) - expect(form.name).to eq 'Leia Organa' + expect(form.name).to eq "Leia Organa" expect(form.age).to eq 20 expect(form.valid?).to be true end - it 'instantiates from params with nil root key' do - params = make_params({ name: 'Leia Organa', age: 20 }) - form = PersonForm.from_params(params, root_key: nil) + it "instantiates from params with nil root key" do + params = make_params({name: "Leia Organa", age: 20}) + form = PersonForm.from_params(params, root_key: nil) - expect(form.name).to eq 'Leia Organa' + expect(form.name).to eq "Leia Organa" expect(form.age).to eq 20 expect(form.valid?).to be true end - it 'instantiates from params with complex data' do + it "instantiates from params with complex data" do friends = %w[Luke Han Chewie] - params = make_params({ friends: friends }) - form = PersonForm.from_params(params) + params = make_params({friends: friends}) + form = PersonForm.from_params(params) expect(form.friends).to eq friends end - it 'instantiates from params with additional data' do - params = make_params({ name: 'Leia Organa' }) + it "instantiates from params with additional data" do + params = make_params({name: "Leia Organa"}) form = PersonForm.from_params(params) assert_nil form.age assert form.invalid? - form = PersonForm.from_params(params, additional_params: { age: 20 }) + form = PersonForm.from_params(params, additional_params: {age: 20}) - expect(form.name).to eq 'Leia Organa' + expect(form.name).to eq "Leia Organa" expect(form.age).to eq 20 expect(form.valid?).to be true end - it 'instantiates from model' do - person = Person.create({ name: 'Han Solo', age: 30 }) + it "instantiates from model" do + person = Person.create({name: "Han Solo", age: 30}) form = PersonForm.from_model(person) - expect(form.name).to eq 'Han Solo' + expect(form.name).to eq "Han Solo" expect(form.age).to eq 30 expect(form.valid?).to be true end - it 'instantiates from model' do - person = Person.create({ name: 'Han Solo', age: 30 }) + it "instantiates from model" do + person = Person.create({name: "Han Solo", age: 30}) form = PersonForm.from_model(person) - expect(form.name).to eq 'Han Solo' + expect(form.name).to eq "Han Solo" expect(form.age).to eq 30 expect(form.valid?).to be true end - it 'instantiates invalid from model' do - person = Person.create({ name: 'Han Solo' }) + it "instantiates invalid from model" do + person = Person.create({name: "Han Solo"}) form = PersonForm.from_model(person) - expect(form.name).to eq 'Han Solo' + expect(form.name).to eq "Han Solo" expect(form.valid?).to be false end - describe '#as_model' do - it 'converts to model' do - person_form = PersonForm.new({ name: 'Luke Skywalker', age: 20 }) + describe "#as_model" do + it "converts to model" do + person_form = PersonForm.new({name: "Luke Skywalker", age: 20}) person = person_form.as_model(Person) expect(person).to be_a Person @@ -123,36 +123,36 @@ expect(person.age).to eq person_form.age end - it 'converts to model with custom methods' do - bad_form = BadForm.new({ name_and_age: 'Luke 20' }) + it "converts to model with custom methods" do + bad_form = BadForm.new({name_and_age: "Luke 20"}) person = bad_form.as_model(Person, { - name: :get_name, - age: :get_age - }) + name: :get_name, + age: :get_age + }) expect(person).to be_a Person expect(person.name).to eq bad_form.get_name expect(person.age).to eq bad_form.get_age end - it 'converts to model with custom mapping' do - person_form = PersonForm.new({ name: 'Luke Skywalker', age: 20 }) + it "converts to model with custom mapping" do + person_form = PersonForm.new({name: "Luke Skywalker", age: 20}) person = person_form.as_model(Person, { - name: :age - }) + name: :age + }) assert person.is_a? Person assert_equal person.name, person_form.age.to_s end end - describe 'associations' do - it 'validates associations' do + describe "associations" do + it "validates associations" do org_form = OrgForm.new({ - owner: PersonForm.new({ - name: 'Yoda' - }) - }) + owner: PersonForm.new({ + name: "Yoda" + }) + }) expect(org_form.valid?).to be false expect(org_form.errors[:owner]).to contain_exactly "age can't be blank" diff --git a/spec/lib/generators/command_generator_test.rb b/spec/lib/generators/command_generator_test.rb index c29094a..716fc8c 100644 --- a/spec/lib/generators/command_generator_test.rb +++ b/spec/lib/generators/command_generator_test.rb @@ -1,17 +1,17 @@ # frozen_string_literal: true -require 'rails/generators/test_case' +require "rails/generators/test_case" module SlayerRails class CommandGeneratorTest < Rails::Generators::TestCase tests Slayer::Generators::CommandGenerator - destination File.expand_path('../../dummy/tmp', File.dirname(__FILE__)) + destination File.expand_path("../../dummy/tmp", File.dirname(__FILE__)) arguments %w[Test] def test_command_skeleton_is_created run_generator - assert_file 'app/commands/test_command.rb', /class TestCommand < Slayer::Command/ + assert_file "app/commands/test_command.rb", /class TestCommand < Slayer::Command/ end end end diff --git a/spec/lib/generators/form_generator_test.rb b/spec/lib/generators/form_generator_test.rb index 254b9cf..f622319 100644 --- a/spec/lib/generators/form_generator_test.rb +++ b/spec/lib/generators/form_generator_test.rb @@ -1,22 +1,22 @@ # frozen_string_literal: true -require 'rails/generators/test_case' +require "rails/generators/test_case" module SlayerRails class FormGeneratorTest < Rails::Generators::TestCase tests Slayer::Generators::FormGenerator - destination File.expand_path('../../dummy/tmp', File.dirname(__FILE__)) + destination File.expand_path("../../dummy/tmp", File.dirname(__FILE__)) def test_form_skeleton_is_created self.class.arguments %w[Test] run_generator - assert_file 'app/forms/test_form.rb', /class TestForm < Slayer::Form/ + assert_file "app/forms/test_form.rb", /class TestForm < Slayer::Form/ end def test_form_attributes_are_created self.class.arguments %w[FieldTest name:String age:Integer comment other] run_generator - assert_file 'app/forms/field_test_form.rb' do |contents| + assert_file "app/forms/field_test_form.rb" do |contents| assert_match(/class FieldTestForm < Slayer::Form/, contents) assert_match(/attribute :name, String/, contents) assert_match(/attribute :age, Integer/, contents) diff --git a/spec/lib/transaction_spec.rb b/spec/lib/transaction_spec.rb index 3ea0682..f8c8970 100644 --- a/spec/lib/transaction_spec.rb +++ b/spec/lib/transaction_spec.rb @@ -1,17 +1,17 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" -describe 'Transactions' do - it 'responds to transaction' do +describe "Transactions" do + it "responds to transaction" do expect(Slayer::Command.new).to respond_to :transaction end - it 'runs transaction block' do + it "runs transaction block" do expect { TransactionCommand.call }.to change { Person.count }.by(1) end - it 'rolls back transaction block' do + it "rolls back transaction block" do allow(Person).to receive(:create).and_raise(:error) expect { TransactionCommand.call }.to change { Person.count }.by(0) diff --git a/spec/lib/translation_spec.rb b/spec/lib/translation_spec.rb index cae3af2..eb1ec01 100644 --- a/spec/lib/translation_spec.rb +++ b/spec/lib/translation_spec.rb @@ -1,49 +1,49 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" # rubocop:disable Metrics/BlockLength -describe 'Translation' do - it 'responds to translate' do +describe "Translation" do + it "responds to translate" do expect(Slayer::Command.new).to respond_to :translate expect(Slayer::Command.new).to respond_to :t end - it 'responds to localize' do + it "responds to localize" do expect(Slayer::Command.new).to respond_to :localize expect(Slayer::Command.new).to respond_to :l end - it 'transates' do - ['commands.transaction.demo', 'services.transaction.demo', 'forms.person.demo'].each do |key| - expected = key.split('.').first.singularize.titleize + it "transates" do + ["commands.transaction.demo", "services.transaction.demo", "forms.person.demo"].each do |key| + expected = key.split(".").first.singularize.titleize expect(TransactionCommand.new.translate(key)).to eq expected expect(PersonForm.new.translate(key)).to eq expected end end - it 'translates with shortcut' do + it "translates with shortcut" do [TransactionCommand, PersonForm].each do |klass| - expected = klass.name.underscore.split('_').last.titleize - expect(klass.new.t('.demo')).to eq expected + expected = klass.name.underscore.split("_").last.titleize + expect(klass.new.t(".demo")).to eq expected end end - it 'transates shortcut for module' do - result = GoodCustomer::AwesomeProduct::RunFooBarCommand.new.translate('.demo') - expect(result).to eq 'It Lives' + it "transates shortcut for module" do + result = GoodCustomer::AwesomeProduct::RunFooBarCommand.new.translate(".demo") + expect(result).to eq "It Lives" end - it 'localizes' do + it "localizes" do [TransactionCommand, PersonForm].each do |klass| - expect(klass.new.l(three_o_clock, format: '%I%p')).to eq '03PM' + expect(klass.new.l(three_o_clock, format: "%I%p")).to eq "03PM" end end private def three_o_clock - Time.parse('3:00pm') + Time.parse("3:00pm") end end # rubocop:enable Metrics/BlockLength diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fa54cec..3eb553a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,17 @@ # frozen_string_literal: true -ENV['RAILS_ENV'] ||= 'test' +ENV["RAILS_ENV"] ||= "test" -require 'rails/all' -require 'dummy/application' -require 'byebug' -require 'rspec/rails' -require 'slayer_rails' +require "rails/all" +require "dummy/application" +require "byebug" +require "rspec/rails" +require "slayer_rails" Dummy::Application.initialize! # Fixtures -Dir['spec/fixtures/**/*.rb'].each { |f| require File.expand_path(f) } +Dir["spec/fixtures/**/*.rb"].each { |f| require File.expand_path(f) } RSpec.configure do |config| config.infer_spec_type_from_file_location! diff --git a/tasks/db.rb b/tasks/db.rb index 0670179..2781015 100644 --- a/tasks/db.rb +++ b/tasks/db.rb @@ -2,52 +2,50 @@ # source: https://gist.github.com/schickling/6762581 -require 'slayer_rails' -require 'active_record' -require 'yaml' +require "slayer_rails" +require "active_record" +require "yaml" -GEM_ROOT = File.dirname(__dir__) -DB_CONFIG = YAML.safe_load(File.open('spec/dummy/config/database.yml')) -SCHEMA_FILE = "#{GEM_ROOT}/spec/dummy/db/schema.rb" +GEM_ROOT = File.dirname(__dir__) +DB_CONFIG = YAML.safe_load(File.open("spec/dummy/config/database.yml")) +SCHEMA_FILE = "#{GEM_ROOT}/spec/dummy/db/schema.rb" MIGRATION_DIR = "#{GEM_ROOT}/spec/dummy/db/migrate" namespace :db do - desc 'Migrate the database' + desc "Migrate the database" task :migrate do ActiveRecord::Base.establish_connection(DB_CONFIG) ActiveRecord::Migrator.migrate(MIGRATION_DIR) - Rake::Task['db:schema'].invoke - puts 'Database migrated.' + Rake::Task["db:schema"].invoke + puts "Database migrated." end - desc 'Create a db/schema.rb file that is portable against any supported DB' + desc "Create a db/schema.rb file that is portable against any supported DB" task :schema do ActiveRecord::Base.establish_connection(DB_CONFIG) - require 'active_record/schema_dumper' + require "active_record/schema_dumper" - File.open(SCHEMA_FILE, 'w:utf-8') do |file| + File.open(SCHEMA_FILE, "w:utf-8") do |file| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) end end end namespace :g do - desc 'Generate migration' + desc "Generate migration" task :migration do - name = ARGV[1] || raise('Specify name: rake g:migration name') - timestamp = Time.now.strftime('%Y%m%d%H%M%S') - path = File.expand_path("#{MIGRATION_DIR}/#{timestamp}_#{name}.rb", __FILE__) + name = ARGV[1] || raise("Specify name: rake g:migration name") + timestamp = Time.now.strftime("%Y%m%d%H%M%S") + path = File.expand_path("#{MIGRATION_DIR}/#{timestamp}_#{name}.rb", __FILE__) - migration_class = name.split('_').map(&:capitalize).join + migration_class = name.split("_").map(&:capitalize).join - File.open(path, 'w') do |file| - file.write <<-EOF.strip_heredoc - class #{migration_class} < ActiveRecord::Migration - def change - end + File.write(path, <<~EOF) + class #{migration_class} < ActiveRecord::Migration[6.0] + def change end - EOF - end + end + EOF puts "Migration #{path} created" abort # needed stop other tasks