-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MNOE-688] Generate relation_id getter and setter for has_one relatio…
…nship - Introduce HasOneExtension, generating getter and setter for has_one relations, making them saved in relationships, and allowing their retrieving from a relations cache - Remove useless timestamp method - Rplace is_integer by is_id that is more semantic - Try to not rely on parent_organization
- Loading branch information
Showing
11 changed files
with
127 additions
and
91 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
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
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,30 @@ | ||
module JsonApiClientExtension::HasOneExtension | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def has_one(attr_name, options = {}) | ||
class_eval <<-CODE | ||
def #{attr_name}_id=(id) | ||
ActiveSupport::Deprecation.warn(self.class.name + ".#{attr_name}_id Use relationships instead") | ||
association = id ? {data: {type: "#{attr_name.to_s.pluralize}", id: id.to_s}} : nil | ||
self.relationships.#{attr_name} = association | ||
end | ||
def #{attr_name}_id | ||
# First we try in the relationship | ||
relationship_definitions = self.relationships.try(:#{attr_name}) | ||
return nil unless relationship_definitions | ||
relationship_definitions.dig(:data, :id) | ||
end | ||
def #{attr_name}=(relation) | ||
self.relationships.#{attr_name} = relation | ||
relations[:#{attr_name}] = relation | ||
end | ||
def #{attr_name} | ||
relations[:#{attr_name}] ||= super | ||
end | ||
CODE | ||
super | ||
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
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