lib/defaults.rb in defaults-1.0.1 vs lib/defaults.rb in defaults-2.0.0

- old
+ new

@@ -1,51 +1,70 @@ module Defaults require "defaults/version" + def self.execute_callable(value, record) + callable = value.is_a?(Proc) ? value : value.method(:call) + callable.arity == 1 ? value.call(record) : value.call + end + + def self.default_value_for_column(model, attribute) + column_info = model.columns_hash[attribute.to_s] + ActiveRecord::Type.lookup(column_info.type).cast(column_info.default) if column_info + end + + def self.default_value(model, record, attribute) + value = model.default_values[attribute] + value = Defaults.execute_callable(value, record) if value.respond_to?(:call) + value + end + + def self.resolve_default_value(model, record, attribute) + attribute = attribute.to_sym + + # Check if value has been defined by `defaults`. + # If it is, try to resolve by checking if value is callable. + # Otherwise returns the default value of that column. + if model.default_values.key?(attribute) + Defaults.default_value(model, record, attribute) + else + Defaults.default_value_for_column(model, attribute) + end + end + def self.included(base) base.extend ClassMethods class << base - attr_accessor :default_options + attr_accessor :default_values end end module ClassMethods - # defaults :title => "Add your title here" def defaults(attrs) - raise ArgumentError, "Hash expected; #{attrs.class} given." unless attrs.kind_of?(Hash) + raise ArgumentError, "Hash expected; #{attrs.class} given." unless attrs.is_a?(Hash) include InstanceMethods - self.default_options ||= {} - self.default_options.merge!(attrs) + self.default_values ||= {} + self.default_values.merge!(attrs) after_initialize :set_default_attributes end end module InstanceMethods def default_for(name) - if self.class.default_options.key?(name.to_sym) - value = self.class.default_options[name.to_sym] - value = value.arity == 1 ? value.call(self) : value.call if value.respond_to?(:call) - else - column_info = self.class.columns_hash[name.to_s] - value = column_info.type_cast_from_user(column_info.default) if column_info - end - - value + Defaults.resolve_default_value(self.class, self, name) end - private - def set_default_attributes + private def set_default_attributes return unless new_record? - self.class.default_options.keys.each do |name| + self.class.default_values.keys.each do |name| value = read_attribute(name) if changes[name] value = default_for(name) if value.blank? - __send__ "#{name}=", value + public_send "#{name}=", value end end end end