Skip to content

Instantly share code, notes, and snippets.

@zerobearing2
Last active December 27, 2015 00:59
Show Gist options
  • Save zerobearing2/7242080 to your computer and use it in GitHub Desktop.
Save zerobearing2/7242080 to your computer and use it in GitHub Desktop.

Revisions

  1. zerobearing2 revised this gist Oct 30, 2013. 1 changed file with 0 additions and 25 deletions.
    25 changes: 0 additions & 25 deletions pg_json_store.rb
    Original 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

    # # 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
  2. zerobearing2 created this gist Oct 30, 2013.
    60 changes: 60 additions & 0 deletions pg_json_store.rb
    Original 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