Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ gem "rubocop", "~> 1.21"

gem "debug"
gem "sqlite3"

gem "actionpack"
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/trenchcoat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand Down
17 changes: 17 additions & 0 deletions test/test_trenchcoat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down