-
-
Save kivanio/250c845812e29602d9f8 to your computer and use it in GitHub Desktop.
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
module ActiveRecord | |
module PgJsonStore | |
extend ActiveSupport::Concern | |
module ClassMethods | |
# | |
# Store (JSON) using Postgres | |
# JSON data type. | |
# | |
# Inspired by: http://api.rubyonrails.org/classes/ActiveRecord/Store.html | |
# | |
# Example: | |
# | |
# class User < ActiveRecord::Base | |
# include ActiveRecord::PgJsonStore | |
# | |
# pg_json_store :address, accessors: [:street1, | |
# :street2, :city, :state, :postal, :country] | |
# end | |
# | |
# User.new(city: "San Diego", state: "CA") | |
# => #<User address: {"city"=>"San Diego", "state"=>"CA"}> | |
# | |
def pg_json_store(column, options = {}) | |
column = column.to_sym | |
accessors = options.delete(:accessors) | |
store_accessor(column, accessors) if accessors | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment