ActiveRecord::Store but with type definition
ActiveRecord::Store but with type definition
ActiveRecord::Store but with typed attributes.
Add this line to your application's Gemfile:
gem 'activerecord-typedstore'And then execute:
$ bundleOr install it yourself as:
$ gem install activerecord-typedstoreIt works exactly like ActiveRecord::Store documentation but you can declare the type of your attributes.
Attributes definition is similar to activerecord's migrations:
…Type casting rules and attribute behavior are exactly the same as for real database columns. Actually the only difference is that you won't be able to query on these attributes (unless you use JSON or Postgres HStore types) and that you don't need to do a migration to add / remove an attribute.
If not, then please fill in an issue.
Just like for store, you can use any custom coder:
module Base64MarshalCoder
extend self
def load(data)
return {} unless data
Marshal.load(Base64.decode64(data))
end
def dump(data)
Base64.encode64(Marshal.dump(data || {}))
end
end
typed_store :settings, coder: Base64MarshalCoder do |s|
# ...
endIf you want to use JSON column or Postgres HStore types, then you can pass in ActiveRecord::TypedStore::IdentityCoder as the coder.
If you want to persist your store in a Postgres HStore, then there is some limitations imposed by the current HStore implementation in Postgres. Since HStore can only store strings:
array attributes won't workany attributes will be converted to stringIf you use HStore because you need to be able to query the store from SQL, and any of these limitations are an issue for you, then you could probably use the JSON column type, which do not suffer from these limitations and is also queriable.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)No open issues yet, or sync has not completed.