trenchcoat 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a247e5d207ed202dee8f4ae60fb16257cf297db730220462acbae5b21389bab
4
- data.tar.gz: ce0e27f3b0ffe5c5579d150ae70b276aaa09a56e70a0da01548508c4544dcfba
3
+ metadata.gz: c4a999d9179ee092b990f45eba56790ffb867fc8c7681e6d1c491e9421794f48
4
+ data.tar.gz: d5598b1853fed58ef5f188feff2633e26ad956ef73a25ede7912e1c014539e6d
5
5
  SHA512:
6
- metadata.gz: 8dd448293b16dd374d4a6c43e2344fa216ae32334055fbe2c2b80f622a36205364703bb4bd97eaba977944f0abe10038945d13ac563bbf7c9624dd53a5209038
7
- data.tar.gz: f8c1da6f9332cab609b1753deb6aece9db3483f630a41ec519e571d62eee49e0db7984faf9137016d6498d60b474722ee994218b1f2f554d6843d961a3eafaa7
6
+ metadata.gz: 4c4905138ac1431360d07532fd681636e2b8b9982d153ce3a891743c17cb073811bf064a86283e3f908ba3adc6831c061fa170a9f52851cf7b4c7bff283337c0
7
+ data.tar.gz: 0b75ccf44c4e0b1a5162dafd466be21d555cf57dc788d81388349b2dcd7cf7d7f07a70a9b598237edf922ee23fb612c805b9db244ee04a16ed7a42ff41533728
data/.rubocop.yml CHANGED
@@ -6,3 +6,7 @@ Style/StringLiterals:
6
6
 
7
7
  Style/StringLiteralsInInterpolation:
8
8
  EnforcedStyle: double_quotes
9
+
10
+ Layout/LineLength:
11
+ Exclude:
12
+ - test/**/*
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2025-08-18
4
+
5
+ - `quack_like` also delegates `new_record?`
6
+ - https://github.com/practical-computer/trenchcoat/pull/5
7
+ - `fallback_to_model_values` normalizes `original_attributes_hash`
8
+ - https://github.com/practical-computer/trenchcoat/pull/4
9
+
10
+ ## [0.2.0] - 2025-05-07
11
+
12
+ - `copy_attribute_definitions` uses `type_for_attribute`, `fallback_to_model_values` checks `against original_attributes_hash`
13
+ - https://github.com/practical-computer/trenchcoat/pull/1
14
+
3
15
  ## [0.1.0] - 2025-05-06
4
16
 
5
17
  - Initial release
data/README.md CHANGED
@@ -2,15 +2,13 @@
2
2
 
3
3
  ## Installation
4
4
 
5
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
6
-
7
5
  Install the gem and add to the application's Gemfile by executing:
8
6
 
9
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
7
+ $ bundle add trenchcoat
10
8
 
11
9
  If bundler is not being used to manage dependencies, install the gem by executing:
12
10
 
13
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
11
+ $ gem install trenchcoat
14
12
 
15
13
  ## Usage
16
14
 
@@ -43,10 +41,9 @@ class CustomForm
43
41
 
44
42
  attr_accessor :post
45
43
 
46
- copy_attribute_definitions(model_class: Post, attributes: %i[title published_at])
44
+ copy_attribute_definitions(model_class: Post, attributes: %i[title body published_at])
47
45
  quack_like(model_instance_attr: :post)
48
46
 
49
- attribute :body, :string # has to be manually defined because there is not a Text type for ActiveModel by default
50
47
  attribute :is_published, :boolean, default: false
51
48
  alias is_published? is_published
52
49
 
@@ -58,8 +55,11 @@ class CustomForm
58
55
  published_at
59
56
 
60
57
  self.post = Post.new if post.blank?
61
-
62
- fallback_to_model_values(model: post, attributes: %i[title body published_at])
58
+ fallback_to_model_values(
59
+ model: post,
60
+ attributes_to_check: %i[title body published_at],
61
+ original_attributes_hash: attributes
62
+ )
63
63
 
64
64
  return if attributes.key?(:is_published)
65
65
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Trenchcoat
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/trenchcoat.rb CHANGED
@@ -8,9 +8,10 @@ module Trenchcoat
8
8
  module Model
9
9
  extend ActiveSupport::Concern
10
10
 
11
- def fallback_to_model_values(model:, attributes:)
12
- attributes.each do |attribute|
13
- next if send(attribute)
11
+ def fallback_to_model_values(model:, attributes_to_check:, original_attributes_hash:)
12
+ indifferent_original_attributes = original_attributes_hash.to_h.with_indifferent_access
13
+ attributes_to_check.each do |attribute|
14
+ next if indifferent_original_attributes.key?(attribute)
14
15
 
15
16
  send(:"#{attribute}=", model.public_send(attribute))
16
17
  end
@@ -22,12 +23,12 @@ module Trenchcoat
22
23
 
23
24
  attributes.each do |attribute_name|
24
25
  column = columns.fetch(attribute_name)
25
- attribute column.name, column.type, default: column.default
26
+ attribute column.name, model_class.type_for_attribute(attribute_name), default: column.default
26
27
  end
27
28
  end
28
29
 
29
30
  def quack_like(model_instance_attr:)
30
- delegate :model_name, :persisted?, :id, to: model_instance_attr
31
+ delegate :model_name, :persisted?, :new_record?, :id, to: model_instance_attr
31
32
  end
32
33
  end
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trenchcoat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Cannon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-05-07 00:00:00.000000000 Z
11
+ date: 2025-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel