-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serializer): serialize hash instead of an active record object (#22
- Loading branch information
1 parent
c2b3120
commit 70dea37
Showing
10 changed files
with
192 additions
and
36 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
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
31 changes: 31 additions & 0 deletions
31
...active_record/lib/forest_admin_datasource_active_record/utils/active_record_serializer.rb
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,31 @@ | ||
module ForestAdminDatasourceActiveRecord | ||
module Utils | ||
ActiveRecordSerializer = Struct.new(:object) do | ||
def to_hash | ||
hash_object(object) | ||
end | ||
|
||
def hash_object(object, with_associations: true) | ||
hash = {} | ||
|
||
return {} if object.nil? | ||
|
||
hash.merge! object.attributes | ||
|
||
if with_associations | ||
each_association_collection(object) do |association_name, item| | ||
hash[association_name] = hash_object(item, with_associations: false) | ||
end | ||
end | ||
|
||
hash | ||
end | ||
|
||
def each_association_collection(object) | ||
one_associations = %i[has_one belongs_to] | ||
object.class.reflect_on_all_associations.filter { |a| one_associations.include?(a.macro) } | ||
.each { |association| yield(association.name.to_s, object.send(association.name.to_s)) } | ||
end | ||
end | ||
end | ||
end |