From 8b88fcad20152e58853d50c234fec7f5f6900347 Mon Sep 17 00:00:00 2001 From: Andy Pike Date: Mon, 8 Aug 2022 16:24:23 +0100 Subject: [PATCH 1/4] Support Ruby 3 --- Gemfile.lock | 2 +- lib/rectify/command.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index bd15933..2c9cc85 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -135,4 +135,4 @@ DEPENDENCIES wisper-rspec BUNDLED WITH - 1.16.2 + 2.2.33 diff --git a/lib/rectify/command.rb b/lib/rectify/command.rb index 684af72..6cd6e44 100644 --- a/lib/rectify/command.rb +++ b/lib/rectify/command.rb @@ -24,7 +24,7 @@ class Command def self.call(*args, &block) event_recorder = EventRecorder.new - command = new(*args) + command = new(**args) command.subscribe(event_recorder) command.evaluate(&block) if block_given? command.call @@ -43,7 +43,7 @@ def transaction(&block) def method_missing(method_name, *args, &block) if @caller.respond_to?(method_name, true) - @caller.send(method_name, *args, &block) + @caller.send(method_name, **args, &block) else super end From 9e1adea28bcfc75b3f92c76f192f5bb4eb538aa1 Mon Sep 17 00:00:00 2001 From: Andy Pike Date: Mon, 8 Aug 2022 20:36:31 +0100 Subject: [PATCH 2/4] Dockerize for Ruby 3 and Rails 7 --- .rubocop.yml | 16 +- .rubocop_todo.yml | 17 +-- Dockerfile | 25 +++ Gemfile.lock | 143 +++++++++--------- Rakefile | 14 +- bin/docker/rspec | 5 + bin/docker/rubocop | 3 + docker-compose.yml | 24 +++ docker-entrypoint.sh | 9 ++ lib/rectify/command.rb | 4 +- lib/rectify/controller_helpers.rb | 2 +- lib/rectify/format_attributes_hash.rb | 6 +- .../rspec/database_reporter/display.rb | 8 +- lib/rectify/rspec/matchers.rb | 4 +- lib/rectify/rspec/stub_form.rb | 2 +- makefile | 25 +++ readme.md | 2 +- rectify.gemspec | 12 +- spec/db/schema.rb | 27 ++-- spec/fixtures/command/args_command.rb | 10 +- spec/fixtures/command/named_args_command.rb | 11 ++ spec/fixtures/queries/users_over_using_sql.rb | 2 +- spec/lib/rectify/command_spec.rb | 15 +- spec/lib/rectify/form_spec.rb | 74 ++++----- spec/spec_helper.rb | 4 +- 25 files changed, 296 insertions(+), 168 deletions(-) create mode 100644 Dockerfile create mode 100755 bin/docker/rspec create mode 100755 bin/docker/rubocop create mode 100644 docker-compose.yml create mode 100755 docker-entrypoint.sh create mode 100644 makefile create mode 100644 spec/fixtures/command/named_args_command.rb diff --git a/.rubocop.yml b/.rubocop.yml index 046a725..f125a6a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,10 +1,22 @@ inherit_from: .rubocop_todo.yml AllCops: + NewCops: enable + SuggestExtensions: false + TargetRubyVersion: 3.0 Exclude: - 'bin/**/*' - 'spec/db/schema.rb' +Lint/MissingSuper: + Enabled: false + +Lint/EmptyBlock: + Enabled: false + +Style/OpenStructUse: + Enabled: false + Style/Documentation: Enabled: false @@ -14,7 +26,7 @@ Style/HashSyntax: Style/StringLiterals: EnforcedStyle: double_quotes -Layout/AlignParameters: +Layout/ParameterAlignment: EnforcedStyle: with_fixed_indentation Layout/MultilineOperationIndentation: @@ -24,7 +36,7 @@ Layout/MultilineMethodCallIndentation: EnforcedStyle: indented Layout/IndentationWidth: - Width: 2 + Width: 2 Metrics/ClassLength: Max: 115 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ff401a2..18ec204 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -8,25 +8,19 @@ # Offense count: 1 # Cop supports --auto-correct. -Lint/UnneededCopDisableDirective: +Lint/RedundantCopDisableDirective: Exclude: - 'lib/rectify/command.rb' # Offense count: 6 # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. # AllowedNames: io, id, to, by, on, in, at -Naming/UncommunicativeMethodParamName: +Lint/MissingSuper: Exclude: - 'lib/rectify/format_attributes_hash.rb' - 'lib/rectify/rspec/database_reporter/reporter.rb' - 'spec/fixtures/command/args_command.rb' -# Offense count: 1 -# Cop supports --auto-correct. -Performance/RegexpMatch: - Exclude: - - 'lib/rectify/rspec/matchers.rb' - # Offense count: 3 # Cop supports --auto-correct. Style/ExpandPathArguments: @@ -47,11 +41,6 @@ Style/IfUnlessModifier: Exclude: - 'lib/rectify/rspec/matchers.rb' -# Offense count: 1 -Style/MethodMissingSuper: - Exclude: - - 'lib/rectify/command.rb' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, EnforcedStyle. @@ -64,5 +53,5 @@ Style/NumericPredicate: # Offense count: 1 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https -Metrics/LineLength: +Layout/LineLength: Max: 100 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..840b2c3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM ruby:3.0.4 + +ENV APP_HOME /app +RUN mkdir -p $APP_HOME +WORKDIR $APP_HOME + +RUN apt-get update && apt-get install -y apt-transport-https --no-install-recommends \ + build-essential \ + ruby-dev \ + libgdbm-dev \ + libncurses5-dev \ + curl \ + vim \ + graphviz \ + && rm -rf /var/lib/apt/lists/* + +COPY . $APP_HOME + +ENV BUNDLE_GEMFILE=$APP_HOME/Gemfile \ + BUNDLE_PATH=/gems \ + BUNDLE_BIN=/gems/bin + +RUN bundle install + +ENTRYPOINT ["/app/docker-entrypoint.sh"] diff --git a/Gemfile.lock b/Gemfile.lock index 2c9cc85..357d316 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,115 +2,120 @@ PATH remote: . specs: rectify (0.13.0) - activemodel (>= 4.1.0) - activerecord (>= 4.1.0) - activesupport (>= 4.1.0) + activemodel (>= 7.0.3) + activerecord (>= 7.0.3) + activesupport (>= 7.0.3) virtus (~> 1.0.5) wisper (>= 1.6.1) GEM remote: https://rubygems.org/ specs: - actionpack (5.2.1) - actionview (= 5.2.1) - activesupport (= 5.2.1) - rack (~> 2.0) + actionpack (7.0.3.1) + actionview (= 7.0.3.1) + activesupport (= 7.0.3.1) + rack (~> 2.0, >= 2.2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.2.1) - activesupport (= 5.2.1) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actionview (7.0.3.1) + activesupport (= 7.0.3.1) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.3) - activemodel (5.2.1) - activesupport (= 5.2.1) - activerecord (5.2.1) - activemodel (= 5.2.1) - activesupport (= 5.2.1) - arel (>= 9.0) - activesupport (5.2.1) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activemodel (7.0.3.1) + activesupport (= 7.0.3.1) + activerecord (7.0.3.1) + activemodel (= 7.0.3.1) + activesupport (= 7.0.3.1) + activesupport (7.0.3.1) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) - minitest (~> 5.1) - tzinfo (~> 1.1) - arel (9.0.0) - ast (2.4.0) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + ast (2.4.2) awesome_print (1.8.0) axiom-types (0.1.1) descendants_tracker (~> 0.0.4) ice_nine (~> 0.11.0) thread_safe (~> 0.3, >= 0.3.1) - builder (3.2.3) + builder (3.2.4) coderay (1.1.2) coercible (1.0.0) descendants_tracker (~> 0.0.1) - concurrent-ruby (1.0.5) - crass (1.0.4) + concurrent-ruby (1.1.10) + crass (1.0.6) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) - diff-lcs (1.3) + diff-lcs (1.5.0) equalizer (0.0.11) - erubi (1.7.1) - i18n (1.1.1) + erubi (1.11.0) + i18n (1.12.0) concurrent-ruby (~> 1.0) ice_nine (0.11.2) - jaro_winkler (1.5.1) - loofah (2.2.2) + json (2.6.2) + loofah (2.18.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) method_source (0.9.0) - mini_portile2 (2.3.0) - minitest (5.11.3) - nokogiri (1.8.5) - mini_portile2 (~> 2.3.0) - parallel (1.12.1) - parser (2.5.1.2) - ast (~> 2.4.0) - powerpack (0.1.2) + mini_portile2 (2.8.0) + minitest (5.16.2) + nokogiri (1.13.8) + mini_portile2 (~> 2.8.0) + racc (~> 1.4) + parallel (1.22.1) + parser (3.1.2.0) + ast (~> 2.4.1) pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) - rack (2.0.5) - rack-test (1.1.0) - rack (>= 1.0, < 3) + racc (1.6.0) + rack (2.2.4) + rack-test (2.0.2) + rack (>= 1.3) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) - rails-html-sanitizer (1.0.4) - loofah (~> 2.2, >= 2.2.2) - rainbow (3.0.0) + rails-html-sanitizer (1.4.3) + loofah (~> 2.3) + rainbow (3.1.1) rake (12.3.1) - rspec (3.8.0) - rspec-core (~> 3.8.0) - rspec-expectations (~> 3.8.0) - rspec-mocks (~> 3.8.0) - rspec-collection_matchers (1.1.3) + regexp_parser (2.5.0) + rexml (3.2.5) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-collection_matchers (1.2.0) rspec-expectations (>= 2.99.0.beta1) - rspec-core (3.8.0) - rspec-support (~> 3.8.0) - rspec-expectations (3.8.2) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-mocks (3.8.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.8.0) - rspec-support (3.8.0) - rubocop (0.59.2) - jaro_winkler (~> 1.5.1) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) + rubocop (1.33.0) + json (~> 2.3) parallel (~> 1.10) - parser (>= 2.5, != 2.5.1.1) - powerpack (~> 0.1) + parser (>= 3.1.0.0) rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.19.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (~> 1.0, >= 1.0.1) - ruby-progressbar (1.10.0) - sqlite3 (1.3.13) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.21.0) + parser (>= 3.1.1.0) + ruby-progressbar (1.11.0) + sqlite3 (1.4.4) thread_safe (0.3.6) - tzinfo (1.2.5) - thread_safe (~> 0.1) - unicode-display_width (1.4.0) + tzinfo (2.0.5) + concurrent-ruby (~> 1.0) + unicode-display_width (2.2.0) virtus (1.0.5) axiom-types (~> 0.1) coercible (~> 1.0) @@ -123,7 +128,7 @@ PLATFORMS ruby DEPENDENCIES - actionpack (>= 4.1.0) + actionpack (>= 7.0.3) awesome_print (~> 1.6) pry (~> 0.11.3) rake @@ -131,7 +136,7 @@ DEPENDENCIES rspec (~> 3.8) rspec-collection_matchers (~> 1.1) rubocop - sqlite3 + sqlite3 (>= 1.4.2) wisper-rspec BUNDLED WITH diff --git a/Rakefile b/Rakefile index fb3fd5d..61dfef7 100644 --- a/Rakefile +++ b/Rakefile @@ -45,14 +45,14 @@ namespace :g do migration_class = name.split("_").map(&:capitalize).join - File.open(path, "w") do |file| - file.write <<-MIGRATION.strip_heredoc - class #{migration_class} < ActiveRecord::Migration[5.2] - def change - end + content = <<~MIGRATION + class #{migration_class} < ActiveRecord::Migration[5.2] + def change end - MIGRATION - end + end + MIGRATION + + File.write(path, content) puts "Migration #{path} created" abort # needed stop other tasks diff --git a/bin/docker/rspec b/bin/docker/rspec new file mode 100755 index 0000000..2d8a6f0 --- /dev/null +++ b/bin/docker/rspec @@ -0,0 +1,5 @@ +#!/bin/sh + +echo start specs tests + +docker-compose run --rm web bash -c "bundle exec rspec $*" diff --git a/bin/docker/rubocop b/bin/docker/rubocop new file mode 100755 index 0000000..7e3977f --- /dev/null +++ b/bin/docker/rubocop @@ -0,0 +1,3 @@ +#!/bin/sh + +docker-compose run --rm -v $(PWD):$(PWD) --no-deps web bash -c "bundle exec rubocop $*" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..606a892 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3.7' + +volumes: + store: + gem_cache: + +x-base: &base + image: rectify_web + build: + context: . + dockerfile: Dockerfile + volumes: + - .:/app + - gem_cache:/gems + +services: + web: + <<: *base + stdin_open: true + tty: true + command: ./bin/rspec + environment: + - EDITOR=vim + - WEBPACKER_DEV_SERVER_HOST=webpack diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..1443f30 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/sh +set -e + +if [ -f tmp/pids/server.pid ]; then + # remove old pids + rm tmp/pids/server.pid +fi + +exec "$@" diff --git a/lib/rectify/command.rb b/lib/rectify/command.rb index 6cd6e44..684af72 100644 --- a/lib/rectify/command.rb +++ b/lib/rectify/command.rb @@ -24,7 +24,7 @@ class Command def self.call(*args, &block) event_recorder = EventRecorder.new - command = new(**args) + command = new(*args) command.subscribe(event_recorder) command.evaluate(&block) if block_given? command.call @@ -43,7 +43,7 @@ def transaction(&block) def method_missing(method_name, *args, &block) if @caller.respond_to?(method_name, true) - @caller.send(method_name, **args, &block) + @caller.send(method_name, *args, &block) else super end diff --git a/lib/rectify/controller_helpers.rb b/lib/rectify/controller_helpers.rb index ff31c55..f2c3919 100644 --- a/lib/rectify/controller_helpers.rb +++ b/lib/rectify/controller_helpers.rb @@ -5,7 +5,7 @@ def self.included(base_class) end def present(presenter, options = {}) - presenter_type = options.fetch(:for) { :template } + presenter_type = options.fetch(:for, :template) presenter.attach_controller(self) rectify_presenters[presenter_type] = presenter diff --git a/lib/rectify/format_attributes_hash.rb b/lib/rectify/format_attributes_hash.rb index b9533a5..109227e 100644 --- a/lib/rectify/format_attributes_hash.rb +++ b/lib/rectify/format_attributes_hash.rb @@ -45,14 +45,14 @@ def convert_hash_keys(value) when Array value.map { |v| convert_hash_keys(v) } when Hash - Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }] + value.to_h { |k, v| [underscore_key(k), convert_hash_keys(v)] } else value end end - def underscore_key(k) - k.to_s.underscore.to_sym + def underscore_key(key) + key.to_s.underscore.to_sym end end end diff --git a/lib/rectify/rspec/database_reporter/display.rb b/lib/rectify/rspec/database_reporter/display.rb index d48615f..b8ea678 100644 --- a/lib/rectify/rspec/database_reporter/display.rb +++ b/lib/rectify/rspec/database_reporter/display.rb @@ -35,9 +35,9 @@ def headers time_header = "Time (s)".rjust(7) "#{target_header} | " \ - "#{type_header} | " \ - "#{queries_header} | " \ - "#{time_header}" + "#{type_header} | " \ + "#{queries_header} | " \ + "#{time_header}" end def rows @@ -53,7 +53,7 @@ def rows def summary puts "" - puts "Database Queries: #{query_stats.total_queries} "\ + puts "Database Queries: #{query_stats.total_queries} " \ "in #{query_stats.total_time}s" end end diff --git a/lib/rectify/rspec/matchers.rb b/lib/rectify/rspec/matchers.rb index 33138ec..e3269b3 100644 --- a/lib/rectify/rspec/matchers.rb +++ b/lib/rectify/rspec/matchers.rb @@ -24,7 +24,7 @@ all_queries = queries.join("\n") "expected the number of queries to be #{expected} " \ - "but there were #{queries.size}.\n\n" \ - "Here are the queries that were made:\n\n#{all_queries}" + "but there were #{queries.size}.\n\n" \ + "Here are the queries that were made:\n\n#{all_queries}" end end diff --git a/lib/rectify/rspec/stub_form.rb b/lib/rectify/rspec/stub_form.rb index 1266c49..b99c329 100644 --- a/lib/rectify/rspec/stub_form.rb +++ b/lib/rectify/rspec/stub_form.rb @@ -18,7 +18,7 @@ def invalid? def method_missing(method_name, *args, &block) if attributes.key?(method_name) attributes[method_name] - elsif method_name.to_s.ends_with?("=") + elsif method_name.to_s.end_with?("=") attribute_name = method_name.to_s.chomp("=").to_sym attributes[attribute_name] = args.first else diff --git a/makefile b/makefile new file mode 100644 index 0000000..7381836 --- /dev/null +++ b/makefile @@ -0,0 +1,25 @@ +build: + docker-compose build web + +serve: + docker-compose up + +s: + docker-compose up + +test: + docker-compose run --rm web bundle exec rubocop + docker-compose run --rm web bundle exec rspec + +rebuild: + docker-compose run --rm web bundle install + docker-compose build web + +bash: + docker-compose run web bash + +rspec: + docker-compose run --rm web bundle exec rspec + +rubocop: + docker-compose run --rm web bundle exec rubocop diff --git a/readme.md b/readme.md index cea9fad..42d5a2b 100644 --- a/readme.md +++ b/readme.md @@ -1051,7 +1051,7 @@ class UsersOverUsingSql < Rectify::Query end def sql - <<-SQL.strip_heredoc + <<~SQL SELECT * FROM users WHERE age > :age diff --git a/rectify.gemspec b/rectify.gemspec index 758c8c4..e48d840 100644 --- a/rectify.gemspec +++ b/rectify.gemspec @@ -11,19 +11,21 @@ Gem::Specification.new do |s| s.homepage = "https://github.com/andypike/rectify" s.license = "MIT" s.require_paths = ["lib"] + s.required_ruby_version = ">= 3.0.0" - s.add_dependency "activemodel", ">= 4.1.0" - s.add_dependency "activerecord", ">= 4.1.0" - s.add_dependency "activesupport", ">= 4.1.0" + s.add_dependency "activemodel", ">= 7.0.3" + s.add_dependency "activerecord", ">= 7.0.3" + s.add_dependency "activesupport", ">= 7.0.3" s.add_dependency "virtus", "~> 1.0.5" s.add_dependency "wisper", ">= 1.6.1" - s.add_development_dependency "actionpack", ">= 4.1.0" + s.add_development_dependency "actionpack", ">= 7.0.3" s.add_development_dependency "awesome_print", "~> 1.6" s.add_development_dependency "pry", "~> 0.11.3" s.add_development_dependency "rake" s.add_development_dependency "rspec", "~> 3.8" s.add_development_dependency "rspec-collection_matchers", "~> 1.1" s.add_development_dependency "rubocop" - s.add_development_dependency "sqlite3" + s.add_development_dependency "sqlite3", ">= 1.4.2" + s.metadata["rubygems_mfa_required"] = "true" end diff --git a/spec/db/schema.rb b/spec/db/schema.rb index adf5e4b..ed4f121 100644 --- a/spec/db/schema.rb +++ b/spec/db/schema.rb @@ -2,42 +2,41 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_05_31_090029) do - +ActiveRecord::Schema[7.0].define(version: 2018_05_31_090029) do create_table "addresses", force: :cascade do |t| t.string "street", default: "", null: false t.string "town", default: "", null: false t.string "city", default: "", null: false t.string "post_code", default: "", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false end create_table "contacts", force: :cascade do |t| t.integer "user_id", null: false t.string "name", default: "", null: false t.string "number", default: "", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.index ["user_id"], name: "index_contacts_on_user_id" end create_table "users", force: :cascade do |t| t.string "first_name", default: "", null: false t.integer "age", default: 0, null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.boolean "active", default: true, null: false t.integer "address_id" - t.datetime "last_logged_in" + t.datetime "last_logged_in", precision: nil t.string "user" end diff --git a/spec/fixtures/command/args_command.rb b/spec/fixtures/command/args_command.rb index 493e811..d0e5ae4 100644 --- a/spec/fixtures/command/args_command.rb +++ b/spec/fixtures/command/args_command.rb @@ -1,3 +1,11 @@ class ArgsCommand < Rectify::Command - def initialize(_, _, _); end + def initialize(one, two, three) + @one = one + @two = two + @three = three + end + + private + + attr_reader :one, :two, :three end diff --git a/spec/fixtures/command/named_args_command.rb b/spec/fixtures/command/named_args_command.rb new file mode 100644 index 0000000..ff1cd0d --- /dev/null +++ b/spec/fixtures/command/named_args_command.rb @@ -0,0 +1,11 @@ +class NamedArgsCommand < Rectify::Command + def initialize(name, height:, location:) + @name = name + @height = height + @location = location + end + + private + + attr_reader :name, :height, :location +end diff --git a/spec/fixtures/queries/users_over_using_sql.rb b/spec/fixtures/queries/users_over_using_sql.rb index 39e2e55..077938c 100644 --- a/spec/fixtures/queries/users_over_using_sql.rb +++ b/spec/fixtures/queries/users_over_using_sql.rb @@ -10,7 +10,7 @@ def model end def sql - <<-SQL.strip_heredoc + <<~SQL SELECT * FROM users WHERE age > :age diff --git a/spec/lib/rectify/command_spec.rb b/spec/lib/rectify/command_spec.rb index 0f428da..37cd4d0 100644 --- a/spec/lib/rectify/command_spec.rb +++ b/spec/lib/rectify/command_spec.rb @@ -26,9 +26,9 @@ events = ReturnMultiEventMultiResultCommand.call expect(events).to eq( - :ok => [1, 2, 3], + :ok => [1, 2, 3], :published => "The command works", - :next => [] + :next => [] ) end end @@ -40,6 +40,17 @@ ArgsCommand.call(:a, :b, :c) end + + it "supports named arguments" do + expect(NamedArgsCommand).to receive(:new).with( + "andy", + :height => 185, + :location => "UK" + ) { instance } + expect(instance).to receive(:call) + + NamedArgsCommand.call("andy", :height => 185, :location => "UK") + end end end diff --git a/spec/lib/rectify/form_spec.rb b/spec/lib/rectify/form_spec.rb index 0452bec..e16d6c4 100644 --- a/spec/lib/rectify/form_spec.rb +++ b/spec/lib/rectify/form_spec.rb @@ -8,7 +8,7 @@ ) expect(form).to have_attributes( - :user => "andy38", + :user => "andy38", :first_name => "Andy", :age => 38 ) @@ -18,7 +18,7 @@ form = UserForm.new(:user => "andy38", :first_name => "Andy", :age => 38) expect(form).to have_attributes( - :user => "andy38", + :user => "andy38", :first_name => "Andy", :age => 38 ) @@ -28,18 +28,18 @@ describe ".from_params" do let(:params) do ActionController::Parameters.new( - "id" => "1", + "id" => "1", "other_id" => "2", "user" => { - "user" => "andy38", + "user" => "andy38", "first_name" => "Andy", - "age" => "38", - "colours" => %w[red blue green], - "file" => ActionDispatch::Http::UploadedFile.new(:tempfile => Tempfile.new("file")), + "age" => "38", + "colours" => %w[red blue green], + "file" => ActionDispatch::Http::UploadedFile.new(:tempfile => Tempfile.new("file")), "address" => { - "street" => "1 High Street", - "town" => "Wimbledon", - "city" => "London", + "street" => "1 High Street", + "town" => "Wimbledon", + "city" => "London", "post_code" => "SW19 1AB" }, "contacts" => [ @@ -55,10 +55,10 @@ form = UserForm.from_params(params) expect(form).to have_attributes( - :user => "andy38", + :user => "andy38", :first_name => "Andy", - :age => 38, - :colours => %w[red blue green] + :age => 38, + :colours => %w[red blue green] ) end @@ -78,9 +78,9 @@ form = UserForm.from_params(params) expect(form.address).to have_attributes( - :street => "1 High Street", - :town => "Wimbledon", - :city => "London", + :street => "1 High Street", + :town => "Wimbledon", + :city => "London", :post_code => "SW19 1AB" ) end @@ -169,10 +169,10 @@ form = ChildForm.from_params(params) expect(form).to have_attributes( - :user => "andy38", + :user => "andy38", :first_name => "Andy", - :age => 38, - :school => "Rutlish" + :age => 38, + :school => "Rutlish" ) end @@ -200,16 +200,16 @@ describe ".from_model" do let(:model) do User.new( - :user => "andy38", + :user => "andy38", :first_name => "Andy", - :age => 38, - :contacts => [ + :age => 38, + :contacts => [ Contact.new(:name => "James", :number => "12345") ], :address => Address.new( - :street => "1 High Street", - :town => "Wimbledon", - :city => "London", + :street => "1 High Street", + :town => "Wimbledon", + :city => "London", :post_code => "SW19 1AB" ), :last_logged_in => Time.new(2016, 1, 30, 9, 30, 0) @@ -220,7 +220,7 @@ form = UserForm.from_model(model) expect(form).to have_attributes( - :user => "andy38", + :user => "andy38", :first_name => "Andy", :age => 38 ) @@ -231,7 +231,7 @@ expect(form.contacts).to have(1).item expect(form.contacts.first).to have_attributes( - :name => "James", + :name => "James", :number => "12345" ) end @@ -240,9 +240,9 @@ form = UserForm.from_model(model) expect(form.address).to have_attributes( - :street => "1 High Street", - :town => "Wimbledon", - :city => "London", + :street => "1 High Street", + :town => "Wimbledon", + :city => "London", :post_code => "SW19 1AB" ) end @@ -273,7 +273,7 @@ form = UserForm.from_json(json) expect(form).to have_attributes( - :user => "andy38", + :user => "andy38", :first_name => "Andy", :age => 38 ) @@ -357,14 +357,14 @@ describe "#attributes_with_values" do it "returns a hash of attributes where their values are non-nil" do form = AddressForm.new( - :id => 1, - :street => "1 High Street", - :town => nil, + :id => 1, + :street => "1 High Street", + :town => nil, :post_code => "GU1 2AB" ) expect(form.attributes_with_values).to eq( - :street => "1 High Street", + :street => "1 High Street", :post_code => "GU1 2AB" ) end @@ -483,7 +483,7 @@ it "returns true" do form = UserForm.new( :first_name => "Andy", - :contacts => [ContactForm.new(:name => "Andy")] + :contacts => [ContactForm.new(:name => "Andy")] ) expect(form).to be_valid @@ -549,7 +549,7 @@ it "assigns a context to array attribute child forms" do form = UserForm.new( :first_name => "Andy", - :contacts => [ContactForm.new(:name => "Andy")] + :contacts => [ContactForm.new(:name => "Andy")] ).with_context(:account_id => 1) expect(form.contacts.first.context.account_id).to eq(1) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e84361c..39ae633 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,7 @@ Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) } Dir["spec/fixtures/**/*.rb"].each { |f| require File.expand_path(f) } -system("rake db:migrate") +system("bundle exec rake db:migrate") db_config = YAML.safe_load(File.open("spec/config/database.yml")) ActiveRecord::Base.establish_connection(db_config) @@ -38,4 +38,4 @@ config.include Rectify::RSpec::Helpers end -Rectify::RSpec::DatabaseReporter.enable +# Rectify::RSpec::DatabaseReporter.enable From 90c4f1479699aa16622a52d3b582aedde04bf0bf Mon Sep 17 00:00:00 2001 From: Andy Pike Date: Tue, 9 Aug 2022 09:54:43 +0100 Subject: [PATCH 3/4] Support Ruby 3 keyword arguments --- lib/rectify/command.rb | 4 ++-- spec/fixtures/command/named_args_command.rb | 12 +++++++++--- spec/lib/rectify/command_spec.rb | 15 ++++++++------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/rectify/command.rb b/lib/rectify/command.rb index 684af72..b7c88e8 100644 --- a/lib/rectify/command.rb +++ b/lib/rectify/command.rb @@ -21,10 +21,10 @@ def respond_to_missing?(_method_name, _include_private = false) class Command include Wisper::Publisher - def self.call(*args, &block) + def self.call(*args, **kwargs, &block) event_recorder = EventRecorder.new - command = new(*args) + command = new(*args, **kwargs) command.subscribe(event_recorder) command.evaluate(&block) if block_given? command.call diff --git a/spec/fixtures/command/named_args_command.rb b/spec/fixtures/command/named_args_command.rb index ff1cd0d..285fb3f 100644 --- a/spec/fixtures/command/named_args_command.rb +++ b/spec/fixtures/command/named_args_command.rb @@ -1,11 +1,17 @@ class NamedArgsCommand < Rectify::Command - def initialize(name, height:, location:) - @name = name + def initialize(first_name, last_name, height:, location:, hobby:) + @first_name = first_name + @last_name = last_name @height = height @location = location + @hobby = hobby + end + + def call + broadcast(:ok, "Hello #{first_name}") end private - attr_reader :name, :height, :location + attr_reader :first_name, :last_name, :height, :location, :hobby end diff --git a/spec/lib/rectify/command_spec.rb b/spec/lib/rectify/command_spec.rb index 37cd4d0..c58125c 100644 --- a/spec/lib/rectify/command_spec.rb +++ b/spec/lib/rectify/command_spec.rb @@ -42,14 +42,15 @@ end it "supports named arguments" do - expect(NamedArgsCommand).to receive(:new).with( - "andy", + NamedArgsCommand.call( + "Andy", + "Pike", :height => 185, - :location => "UK" - ) { instance } - expect(instance).to receive(:call) - - NamedArgsCommand.call("andy", :height => 185, :location => "UK") + :location => "UK", + :hobby => "Running" + ) do + on(:ok) { |message| expect(message).to eq("Hello Andy") } + end end end end From a3231288e2db64d464d7caae500eaae9c7a43e9a Mon Sep 17 00:00:00 2001 From: Andy Pike Date: Tue, 9 Aug 2022 09:59:09 +0100 Subject: [PATCH 4/4] Bump version --- Gemfile.lock | 2 +- lib/rectify/version.rb | 2 +- makefile | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 357d316..2ddae3b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rectify (0.13.0) + rectify (0.14.0) activemodel (>= 7.0.3) activerecord (>= 7.0.3) activesupport (>= 7.0.3) diff --git a/lib/rectify/version.rb b/lib/rectify/version.rb index 5986ed7..f8591af 100644 --- a/lib/rectify/version.rb +++ b/lib/rectify/version.rb @@ -1,3 +1,3 @@ module Rectify - VERSION = "0.13.0".freeze + VERSION = "0.14.0".freeze end diff --git a/makefile b/makefile index 7381836..903a6b2 100644 --- a/makefile +++ b/makefile @@ -23,3 +23,7 @@ rspec: rubocop: docker-compose run --rm web bundle exec rubocop + +release: + docker-compose run --rm web bundle install + docker-compose run --rm web gem build rectify.gemspec