{"action":"created","issue":{"url":"https://api.github.com/repos/jrochkind/json_attribute/issues/3","repository_url":"https://api.github.com/repos/jrochkind/json_attribute","labels_url":"https://api.github.com/repos/jrochkind/json_attribute/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/jrochkind/json_attribute/issues/3/comments","events_url":"https://api.github.com/repos/jrochkind/json_attribute/issues/3/events","html_url":"https://github.com/jrochkind/json_attribute/issues/3","id":245567465,"number":3,"title":"Custom attribute type error","user":{"login":"onomated","id":1012979,"avatar_url":"https://avatars3.githubusercontent.com/u/1012979?v=4","gravatar_id":"","url":"https://api.github.com/users/onomated","html_url":"https://github.com/onomated","followers_url":"https://api.github.com/users/onomated/followers","following_url":"https://api.github.com/users/onomated/following{/other_user}","gists_url":"https://api.github.com/users/onomated/gists{/gist_id}","starred_url":"https://api.github.com/users/onomated/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/onomated/subscriptions","organizations_url":"https://api.github.com/users/onomated/orgs","repos_url":"https://api.github.com/users/onomated/repos","events_url":"https://api.github.com/users/onomated/events{/privacy}","received_events_url":"https://api.github.com/users/onomated/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2017-07-25T23:52:27Z","updated_at":"2017-07-28T19:14:44Z","closed_at":null,"body":"I get the following error\r\n```\r\nArgumentError:\r\nSecond argument (Db::Dimensions) must be a symbol or ActiveModel::Type::Value subclass\r\n```\r\nfor a class that is a subclass of `ActiveModel::Type::Value`\r\n\r\nI have a device model defined as:\r\n```ruby\r\nclass Device < ApplicationRecord\r\n  include JsonAttribute::Record\r\n\r\n  json_attribute :size, Db::Dimensions\r\n  # This fails also\r\n  # json_attribute :size, :db_dimensions\r\nend\r\n```\r\n\r\n`Db::Dimensions` subclasses `ActiveRecord::Type::Value` and is defined as\r\n```ruby\r\nmodule Db\r\n  class Dimensions < ActiveRecord::Type::Value\r\n    include ActiveModel::Type::Helpers::Mutable\r\n\r\n    def deserialize(value)\r\n      case value\r\n      when ::String\r\n        return if value.blank?\r\n\r\n        width, height = value.split('x')\r\n        build_dimensions(width, height)\r\n      when ::Array\r\n        build_dimensions(*value)\r\n      else\r\n        value\r\n      end\r\n    end\r\n\r\n    def serialize(value)\r\n      case value\r\n      when Dimensions\r\n        \"#{number_for_dimensions(value.x)}x#{number_for_dimensions(value.y)}\"\r\n      when ::Array\r\n        serialize(build_dimensions(*value))\r\n      else\r\n        super\r\n      end\r\n    end\r\n\r\n    private\r\n\r\n    def number_for_dimensions(number)\r\n      number.to_s.gsub(/\\.0$/, '')\r\n    end\r\n\r\n    def build_dimensions(width, height)\r\n      ::Dimensions.new(Float(width), Float(height))\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nThis is registered in an initializer:\r\n```ruby\r\nActiveRecord::Type.register(:db_dimensions, Db::Dimensions)\r\n```\r\n\r\nAm I missing something here?  I know this can probably be better implemented as a `JsonAttribute::Model`, just wanted to point this out since the readme states that custom types are supported as well."},"comment":{"url":"https://api.github.com/repos/jrochkind/json_attribute/issues/comments/318739404","html_url":"https://github.com/jrochkind/json_attribute/issues/3#issuecomment-318739404","issue_url":"https://api.github.com/repos/jrochkind/json_attribute/issues/3","id":318739404,"user":{"login":"jrochkind","id":149304,"avatar_url":"https://avatars3.githubusercontent.com/u/149304?v=4","gravatar_id":"","url":"https://api.github.com/users/jrochkind","html_url":"https://github.com/jrochkind","followers_url":"https://api.github.com/users/jrochkind/followers","following_url":"https://api.github.com/users/jrochkind/following{/other_user}","gists_url":"https://api.github.com/users/jrochkind/gists{/gist_id}","starred_url":"https://api.github.com/users/jrochkind/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jrochkind/subscriptions","organizations_url":"https://api.github.com/users/jrochkind/orgs","repos_url":"https://api.github.com/users/jrochkind/repos","events_url":"https://api.github.com/users/jrochkind/events{/privacy}","received_events_url":"https://api.github.com/users/jrochkind/received_events","type":"User","site_admin":false},"created_at":"2017-07-28T19:14:44Z","updated_at":"2017-07-28T19:14:44Z","body":"@onomated if you would like to Pull Request that type for inclusion with the gem, I'd be happy to look at it! It should have:\r\n\r\n* Call it OpenHash or something like that maybe?\r\n* Maybe some inline comments about how you choose where to use `deserialize_indifferent_access` and where to use `deserialize_plain`\r\n* A spec\r\n* Ideally it would work well with the `jsonb_where` querying, but not neccesarily required. But mess with it and see if it does, and note that it does not in header comments if it doesn't. Also header comments with examples of use, and explain if it supports string/symbol indifferentness as it seems to?\r\n"}}
{"actor":{"display_login":"jrochkind"}}