Module: CustomFields::Target::ClassMethods
- Defined in:
- lib/custom_fields/target.rb
Instance Method Summary collapse
-
#build_klass_with_custom_fields(recipe) ⇒ Class
Builds the custom klass by sub-classing it from its parent and by applying a recipe.
-
#klass_with_custom_fields(recipe) ⇒ Class
Returns a custom klass always up-to-date.
- #safe_module_parent ⇒ Object
-
#with_custom_fields? ⇒ Boolean
A document with custom fields always returns true.
Instance Method Details
#build_klass_with_custom_fields(recipe) ⇒ Class
Builds the custom klass by sub-classing it from its parent and by applying a recipe
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/custom_fields/target.rb', line 37 def build_klass_with_custom_fields(recipe) name = recipe['name'] # puts "CREATING #{name}, #{recipe.inspect}" # DEBUG safe_module_parent.const_set(name, Class.new(self)).tap do |klass| klass.cattr_accessor :version klass.version = recipe['version'] # copy scopes from the parent class (scopes does not inherit automatically from the parents in mongoid) # FIXME (Did): not needed anymore ? # klass.write_inheritable_attribute(:scopes, self.scopes) recipe['rules'].each do |rule| send(:"apply_#{rule['type']}_custom_field", klass, rule) end recipe_model_name = recipe['model_name'] model_name = proc 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 |
#klass_with_custom_fields(recipe) ⇒ Class
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. The recipe also contains the name which will be assigned to the custom klass.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/custom_fields/target.rb', line 74 def klass_with_custom_fields(recipe) return self if recipe.blank? # no recipe provided name = recipe['name'] (modules = self.name.split('::')).pop parent = modules.empty? ? Object : modules.join('::').constantize klass = parent.const_defined?(name) ? parent.const_get(name) : nil if klass.nil? || klass.version != recipe['version'] # no klass or out-dated klass parent.send(:remove_const, name) if klass klass = build_klass_with_custom_fields(recipe) end klass end |
#safe_module_parent ⇒ Object
94 95 96 |
# File 'lib/custom_fields/target.rb', line 94 def safe_module_parent respond_to?(:module_parent) ? module_parent : parent end |
#with_custom_fields? ⇒ Boolean
A document with custom fields always returns true.
26 27 28 |
# File 'lib/custom_fields/target.rb', line 26 def with_custom_fields? true end |