Sha256: 03d327f97e46440b5843617776e453753aeeb28243b527393ffc0ef93e392fc5
Contents?: true
Size: 1.57 KB
Versions: 1
Compression:
Stored size: 1.57 KB
Contents
require "ar_default_values/version" 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 rating: 'r18', type: 'comic' # 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 type: 'comic', released_at: lambda { Time.now } # end # # or with block: # class Book < ActiveRecord::Base # default_values type: 'comic' do # t = Time.now # {released_at: t, edition_updated_at: t} # end # end # # book1 = Book.new # # wait 10 sec # book2 = Book.new # book1.release_at == book2.release_at # => false # book1.released_at == book1.edition_updated_at # => true # module DefaultValues extend ActiveSupport::Concern module ClassMethods def default_values(defaults = {}, &block) define_method(:initialize_with_default_values) do |*attributes, &inner_block| defaults = defaults.merge(block.call) if block_given? defaults = Hash[defaults.each_pair.map{ |key, value| [key, value.kind_of?(Proc) ? value.call : value]}] initialize_without_default_values(*attributes, &inner_block) defaults.each_pair do |key, value| write_attribute(key, value) unless attribute_present?(key) end end alias_method_chain :initialize, :default_values end end end ActiveRecord::Base.send(:include, DefaultValues)
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ar_default_values-0.4.0 | lib/ar_default_values.rb |