Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require "store_model/ext/parent_assignment" | ||
|
||
module StoreModel | ||
# ActiveModel::Attributes patch with parent tracking support | ||
module Attributes | ||
include ParentAssignment | ||
|
||
private | ||
|
||
def attribute(*) | ||
super.tap do |value| | ||
assign_parent_to_store_model_relation(value) | ||
end | ||
end | ||
|
||
def write_attribute(*) | ||
super.tap do |value| | ||
assign_parent_to_store_model_relation(value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "store_model/ext/parent_assignment" | ||
|
||
module StoreModel | ||
# ActiveRecord::Base patch with parent tracking support | ||
module Base | ||
include ParentAssignment | ||
|
||
def _read_attribute(*) | ||
super.tap do |attribute| | ||
assign_parent_to_store_model_relation(attribute) | ||
end | ||
end | ||
|
||
def _write_attribute(*) | ||
super.tap do |attribute| | ||
assign_parent_to_store_model_relation(attribute) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module StoreModel | ||
# Helper methods for ActiveModel and ActiveRecord parent tracking support | ||
module ParentAssignment | ||
private | ||
|
||
def assign_parent_to_store_model_relation(attribute) | ||
assign_parent_to_singular_store_model(attribute) | ||
return unless attribute.is_a?(Array) | ||
|
||
attribute.each(&method(:assign_parent_to_singular_store_model)) | ||
end | ||
|
||
def assign_parent_to_singular_store_model(item) | ||
item.parent = self if item.is_a?(StoreModel::Model) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "store_model/ext/active_model/attributes" | ||
require "store_model/ext/active_record/base" | ||
|
||
module StoreModel # :nodoc: | ||
class Railtie < Rails::Railtie # :nodoc: | ||
config.to_prepare do |_app| | ||
ActiveSupport.on_load(:active_record) do | ||
ActiveModel::Attributes.prepend(Attributes) | ||
prepend(Base) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters