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
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ Style/StringLiterals:

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Layout/LineLength:
Exclude:
- test/**/*
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
trenchcoat (0.1.0)
trenchcoat (0.2.0)
activemodel (>= 7.0)
activerecord (>= 7.0)
activesupport (>= 7.0)
Expand Down
8 changes: 4 additions & 4 deletions lib/trenchcoat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/trenchcoat/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Trenchcoat
VERSION = "0.1.0"
VERSION = "0.2.0"
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions test/test_trenchcoat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down