lib/custom_fields/target.rb in custom_fields-2.1.0 vs lib/custom_fields/target.rb in custom_fields-2.2.0

- old
+ new

@@ -5,19 +5,19 @@ extend ActiveSupport::Concern included do ## types ## - %w(default string text date boolean file select belongs_to has_many many_to_many).each do |type| - include "CustomFields::Types::#{type.classify}::Target".constantize + %w(default string text email date boolean file select float integer money + belongs_to has_many many_to_many tags).each do |type| + include "CustomFields::Types::#{type.camelize}::Target".constantize end include ::CustomFields::TargetHelpers ## fields ## - field :custom_fields_recipe, :type => Hash - + field :custom_fields_recipe, type: Hash end module ClassMethods # A document with custom fields always returns true. @@ -31,17 +31,15 @@ # Builds the custom klass by sub-classing it # from its parent and by applying a recipe # # @param [ Hash ] recipe The recipe describing the fields to add # - # @return [ Class] the anonymous custom klass + # @return [ Class ] the anonymous custom klass # def build_klass_with_custom_fields(recipe) name = recipe['name'] - # puts "CREATING #{name}, #{recipe.inspect}" # DEBUG - parent.const_set(name, Class.new(self)).tap do |klass| klass.cattr_accessor :version klass.version = recipe['version'] @@ -50,10 +48,20 @@ # klass.write_inheritable_attribute(:scopes, self.scopes) recipe['rules'].each do |rule| self.send(:"apply_#{rule['type']}_custom_field", klass, rule) end + recipe_model_name = recipe['model_name'] + model_name = Proc.new do + if recipe_model_name.is_a?(ActiveModel::Name) + recipe_model_name + else + recipe_model_name.constantize.model_name + end + end + klass.send :define_method, :model_name, model_name + klass.send :define_singleton_method, :model_name, model_name end end # Returns a custom klass always up-to-date. If it does not # exist or if the version is out-dates then build a new custom klass. @@ -84,6 +92,6 @@ end end -end \ No newline at end of file +end