diff --git a/Gemfile b/Gemfile index f25683b..d8f7789 100644 --- a/Gemfile +++ b/Gemfile @@ -13,3 +13,5 @@ gem "rubocop", "~> 1.21" gem "debug" gem "sqlite3" + +gem "actionpack" diff --git a/Gemfile.lock b/Gemfile.lock index d5c55ce..79236a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,6 +9,22 @@ PATH GEM remote: https://rubygems.org/ specs: + actionpack (8.0.2) + actionview (= 8.0.2) + activesupport (= 8.0.2) + nokogiri (>= 1.8.5) + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + useragent (~> 0.16) + actionview (8.0.2) + activesupport (= 8.0.2) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) activemodel (8.0.2) activesupport (= 8.0.2) activerecord (8.0.2) @@ -32,13 +48,16 @@ GEM base64 (0.2.0) benchmark (0.4.0) bigdecimal (3.1.9) + builder (3.3.0) concurrent-ruby (1.3.5) connection_pool (2.5.3) + crass (1.0.6) date (3.4.1) debug (1.10.0) irb (~> 1.10) reline (>= 0.3.8) drb (2.2.1) + erubi (1.13.1) i18n (1.14.7) concurrent-ruby (~> 1.0) io-console (0.8.0) @@ -50,8 +69,16 @@ GEM language_server-protocol (3.17.0.4) lint_roller (1.1.0) logger (1.7.0) + loofah (2.24.1) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) mini_portile2 (2.8.8) minitest (5.25.5) + nokogiri (1.18.9) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) + nokogiri (1.18.9-arm64-darwin) + racc (~> 1.4) parallel (1.27.0) parser (3.3.8.0) ast (~> 2.4.1) @@ -64,6 +91,19 @@ GEM date stringio racc (1.8.1) + rack (3.2.0) + rack-session (2.1.1) + base64 (>= 0.1.0) + rack (>= 3.0.0) + rack-test (2.2.0) + rack (>= 1.3) + rails-dom-testing (2.3.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.2) + loofah (~> 2.21) + nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) rainbow (3.1.1) rake (13.2.1) rdoc (6.13.1) @@ -98,12 +138,14 @@ GEM unicode-emoji (~> 4.0, >= 4.0.4) unicode-emoji (4.0.4) uri (1.0.3) + useragent (0.16.11) PLATFORMS arm64-darwin-23 ruby DEPENDENCIES + actionpack debug minitest (~> 5.16) rake (~> 13.0) diff --git a/lib/trenchcoat.rb b/lib/trenchcoat.rb index 670f8ed..822108b 100644 --- a/lib/trenchcoat.rb +++ b/lib/trenchcoat.rb @@ -9,8 +9,9 @@ module Model extend ActiveSupport::Concern def fallback_to_model_values(model:, attributes_to_check:, original_attributes_hash:) + indifferent_original_attributes = original_attributes_hash.to_h.with_indifferent_access attributes_to_check.each do |attribute| - next if original_attributes_hash.with_indifferent_access.key?(attribute) + next if indifferent_original_attributes.key?(attribute) send(:"#{attribute}=", model.public_send(attribute)) end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3c21cb7..434da45 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,6 +3,7 @@ $LOAD_PATH.unshift File.expand_path("../lib", __dir__) require "trenchcoat" require "active_record" +require "action_controller" require "debug" ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") diff --git a/test/test_trenchcoat.rb b/test/test_trenchcoat.rb index 29fda4f..e168ee7 100644 --- a/test/test_trenchcoat.rb +++ b/test/test_trenchcoat.rb @@ -111,6 +111,23 @@ def normalize_published_at assert_equal true, form.is_published end + test "fallback_to_model_values: given ActionController::Parameters" do + time = Time.utc(2021, 2, 2) + post = Post.create!(title: "A Post", body: "Post Content") + + parameters = ActionController::Parameters.new(title: "Hello", published_at: time, is_published: "1").permit( + :title, :body, :published_at + ).merge(post: post) + + form = CustomForm.new(parameters) + + assert_equal post, form.post + assert_equal "Hello", form.title + assert_equal "Post Content", form.body + assert_equal time, form.published_at + assert_equal true, form.is_published + end + test "creating a new record, updating" do form = CustomForm.new form.title = SecureRandom.hex