Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert JSON(B) column value to hash after reading from repository #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion lib/shrine/plugins/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def initialize(name, **options)

module_eval <<-RUBY, __FILE__, __LINE__ + 1
module EntitySupport
attr_reader :attributes
def initialize(attributes)
attachment = attributes[:#{name}]
@_#{name} = attachment
Expand Down Expand Up @@ -123,6 +122,18 @@ def delete_attachments(id)
end
end
end

module AttacherMethods
private

def convert_after_read(value)
sequel_json_value?(value) ? value.to_hash : super
end

def sequel_json_value?(value)
defined?(Sequel::Postgres::JSONHashBase) && value.is_a?(Sequel::Postgres::JSONHashBase)
end
end
end

register_plugin(:hanami, Hanami)
Expand Down