Sha256: 847cc3ae15ec210fe7ac7f56e561111efd104755d2ca5add02d850d59056778d
Contents?: true
Size: 1.37 KB
Versions: 2
Compression:
Stored size: 1.37 KB
Contents
require 'active_support' require 'active_record' # # = ActiveRecord::DefaultValue # initialize with default_values # # == sample # you can specify default values with hash: # # class Book < ActiveRecord::Base # default_values do # { # :rating => 'r18', # :type => 'comic' # } # end # end # # book = Book.new # book.rating # => "r18" # book.type # => "comic" # book.title # => nil # # you can use instance values with lambda: # # class Book < ActiveRecord::Base # default_values do # lambda do # { # :type => 'comic', # :released_at => Time.now # } # end # end # end # # book1 = Book.new # # wait 10 sec # book2 = Book.new # book1.release_at == book2.release_at # => false # module ActiveRecord module DefaultValue extend ActiveSupport::Concern module ClassMethods def default_values(&block) define_method(:initialize_with_default_values) do |*attributes, &inner_block| hash_or_lambda = block.call hash_or_lambda = hash_or_lambda.call if hash_or_lambda.is_a? Proc attributes[0] = hash_or_lambda.merge(attributes.first || {}) initialize_without_default_values(*attributes, &inner_block) end alias_method_chain :initialize, :default_values end end end end ActiveRecord::Base.send(:include, ActiveRecord::DefaultValue)
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ar_default_values-0.1.1 | lib/ar_default_values.rb |
ar_default_values-0.1.0 | lib/ar_default_values.rb |