Last active
December 27, 2015 00:59
-
-
Save zerobearing2/7242080 to your computer and use it in GitHub Desktop.
Revisions
-
zerobearing2 revised this gist
Oct 30, 2013 . 1 changed file with 0 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,31 +27,6 @@ def pg_json_store(column, options = {}) accessors = options.delete(:accessors) store_accessor(column, accessors) if accessors end end -
zerobearing2 created this gist
Oct 30, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ 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 # # def address # # read_attribute(:address) || Hash.new # # end # define_method("#{column}") do # read_attribute(column) || Hash.new # end # [*accessors].each do |accessor| # accessor = accessor.to_sym # # def city # # read_store_attribute(:address, 'city') # # end # define_method("#{accessor}") do # read_store_attribute(column, accessor) # end # # def city=(val) # # write_store_attribute(:address, 'city', val) # # end # define_method("#{accessor}=") do |val| # write_store_attribute(column, accessor, val) # end # end end end end end