diff --git a/.rubocop.yml b/.rubocop.yml index 762eebb..dd3538c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,3 +6,7 @@ Style/StringLiterals: Style/StringLiteralsInInterpolation: EnforcedStyle: double_quotes + +Layout/LineLength: + Exclude: + - test/**/* \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index a8a2b42..d5c55ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - trenchcoat (0.1.0) + trenchcoat (0.2.0) activemodel (>= 7.0) activerecord (>= 7.0) activesupport (>= 7.0) diff --git a/lib/trenchcoat.rb b/lib/trenchcoat.rb index 370bbe5..670f8ed 100644 --- a/lib/trenchcoat.rb +++ b/lib/trenchcoat.rb @@ -8,9 +8,9 @@ module Trenchcoat module Model extend ActiveSupport::Concern - def fallback_to_model_values(model:, attributes:) - attributes.each do |attribute| - next if send(attribute) + def fallback_to_model_values(model:, attributes_to_check:, original_attributes_hash:) + attributes_to_check.each do |attribute| + next if original_attributes_hash.with_indifferent_access.key?(attribute) send(:"#{attribute}=", model.public_send(attribute)) end @@ -22,7 +22,7 @@ def copy_attribute_definitions(model_class:, attributes:) attributes.each do |attribute_name| column = columns.fetch(attribute_name) - attribute column.name, column.type, default: column.default + attribute column.name, model_class.type_for_attribute(attribute_name), default: column.default end end diff --git a/lib/trenchcoat/version.rb b/lib/trenchcoat/version.rb index 3c2f561..4a1b154 100644 --- a/lib/trenchcoat/version.rb +++ b/lib/trenchcoat/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Trenchcoat - VERSION = "0.1.0" + VERSION = "0.2.0" end diff --git a/test/test_helper.rb b/test/test_helper.rb index 9b76d89..3c21cb7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,7 +10,7 @@ ActiveRecord::Schema.define do create_table :posts do |t| t.string :title, null: false - t.text :body, null: false + t.text :body, null: false, default: "It was a dark and stormy night" t.datetime :published_at end end diff --git a/test/test_trenchcoat.rb b/test/test_trenchcoat.rb index 0fb5e7e..29fda4f 100644 --- a/test/test_trenchcoat.rb +++ b/test/test_trenchcoat.rb @@ -18,10 +18,9 @@ class CustomForm attr_accessor :post - copy_attribute_definitions(model_class: Post, attributes: %i[title published_at]) + copy_attribute_definitions(model_class: Post, attributes: %i[title body published_at]) quack_like(model_instance_attr: :post) - attribute :body, :string # has to be manually defined because there is not a Text type for ActiveModel by default attribute :is_published, :boolean, default: false alias is_published? is_published @@ -33,8 +32,11 @@ def initialize(attributes = {}) published_at self.post = Post.new if post.blank? - - fallback_to_model_values(model: post, attributes: %i[title body published_at]) + fallback_to_model_values( + model: post, + attributes_to_check: %i[title body published_at], + original_attributes_hash: attributes + ) return if attributes.key?(:is_published) @@ -70,7 +72,7 @@ def normalize_published_at assert_equal true, form.post.new_record? assert_nil form.title - assert_nil form.body + assert_equal "It was a dark and stormy night", form.body assert_nil form.published_at assert_equal false, form.is_published end @@ -86,7 +88,7 @@ def normalize_published_at assert_equal true, form.is_published end - test "fallback_to_model_values: params given, falls back to model instance if falsey parameter" do + test "fallback_to_model_values: params given, falls back to model instance if original_attributes_hash did not include parameter" do time = Time.utc(2021, 2, 2) post = Post.create!(title: "A Post", body: "Post Content") form = CustomForm.new(post: post, title: "Hello", published_at: time, is_published: "1")