Created
September 13, 2012 09:11
-
-
Save linjunpop/3713094 to your computer and use it in GitHub Desktop.
metadata fields
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 characters
| >> p = Product.create(:name => 'p1', :foobar => true) | |
| >> p.name | |
| => "p1" | |
| >> p.foobar | |
| => true |
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 characters
| module MetaField | |
| def self.included(base) | |
| base.extend(ClassMethods) | |
| end | |
| module ClassMethods | |
| def meta_field(field, options={}) | |
| type = options.fetch(:type, String) | |
| required = options.fetch(:required, false) | |
| default = options.fetch(:default, nil) | |
| self.send(:validates_presence_of, field) if required | |
| define_method field do | |
| self.metadata[field] | |
| end | |
| define_method "#{field}=" do |value| | |
| self.metadata[field] = Kernel.const_get(type.to_s.classify).new(value || default) | |
| end | |
| end | |
| end | |
| def metadata | |
| self.read_attribute(:metadata) || {} | |
| end | |
| end |
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 characters
| class Product < ActiveRecord::Base | |
| serialize :metadata, Hash | |
| include MetaField | |
| meta_field :name, :type => String | |
| meta_field :foobar, :type => Boolean, :required => true | |
| end |
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 characters
| create_table "products", :force => true do |t| | |
| t.text "metadata" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment