Module: HasCustomFields::ClassMethods
- Defined in:
- lib/has_custom_fields/class_methods.rb
Instance Method Summary collapse
- #custom_field_fields(scope, scope_id) ⇒ Object
-
#has_custom_fields(options = {}) ⇒ Object
Will make the current class have eav behaviour.
Instance Method Details
#custom_field_fields(scope, scope_id) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/has_custom_fields/class_methods.rb', line 124 def custom_field_fields(scope, scope_id) = [self.name] klass = Object.const_get([:fields_class_name]) begin return klass.send("find_all_by_#{scope}_id", scope_id, :order => :id) rescue NoMethodError parent_class = klass.to_s.sub('Field', '') raise InvalidScopeError, "Class #{parent_class} does not have scope :#{scope} defined for has_custom_fields" end end |
#has_custom_fields(options = {}) ⇒ Object
Will make the current class have eav behaviour.
The following options are available on for has_custom_fields to modify the behavior. Reasonable defaults are provided:
-
value_class_name
: The class for the related model. This defaults to the model name prepended to “Attribute”. So for a “User” model the class name would be “UserAttribute”. The class can actually exist (in that case the model file will be loaded through Rails dependency system) or if it does not exist a basic model will be dynamically defined for you. This allows you to implement custom methods on the related class by simply defining the class manually. -
table_name
: The table for the related model. This defaults to the attribute model’s table name. -
relationship_name
: This is the name of the actual has_many relationship. Most of the type this relationship will only be used indirectly but it is there if the user wants more raw access. This defaults to the class name underscored then pluralized finally turned into a symbol. -
foreign_key
: The key in the attribute table to relate back to the model. This defaults to the model name underscored prepended to “_id” -
name_field
: The field which stores the name of the attribute in the related object -
value_field
: The field that stores the value in the related object
34 35 36 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/has_custom_fields/class_methods.rb', line 34 def has_custom_fields( = {}) unless [:scopes].respond_to?(:each) raise ArgumentError, 'Must define :scope => [] on the has_custom_fields class method' end # Provide default options [:fields_class_name] ||= self.name + 'Field' [:fields_table_name] ||= [:fields_class_name].tableize [:fields_relationship_name] ||= [:fields_class_name].underscore.to_sym [:values_class_name] ||= self.name + 'Attribute' [:values_table_name] ||= [:values_class_name].tableize [:relationship_name] ||= [:values_class_name].tableize.to_sym [:select_options_class_name] ||= self.name + "FieldSelectOption" [:select_options_table_name] ||= [:select_options_class_name].tableize [:select_options_relationship_name] ||= [:select_options_class_name].pluralize.underscore.to_sym [:foreign_key] ||= self.name.foreign_key [:base_foreign_key] ||= self.name.underscore.foreign_key [:name_field] ||= 'name' [:value_field] ||= 'value' [:parent] = self.name HasCustomFields.log(:debug, "OPTIONS: #{.inspect}") # Init option storage if necessary cattr_accessor :custom_field_options self. ||= Hash.new # Return if already processed. return if self..keys.include? [:values_class_name] # Attempt to load ModelField related class. If not create it begin Object.const_get([:fields_class_name]) rescue HasCustomFields.create_associated_fields_class() end # Attempt to load ModelAttribute related class. If not create it begin Object.const_get([:values_class_name]) rescue HasCustomFields.create_associated_values_class() end # Attempt to load ModelFieldSelectOption related class. If not create it begin Object.const_get([:select_options_class_name]) rescue HasCustomFields.() end # Store options self.[self.name] = # Modify attribute class attribute_class = Object.const_get([:values_class_name]) base_class = self.name.underscore.to_sym attribute_class.class_eval do belongs_to base_class, :foreign_key => [:base_foreign_key] alias_method :base, base_class # For generic access end # Modify main class class_eval do attr_accessible :custom_fields has_many [:relationship_name], :class_name => [:values_class_name], :table_name => [:values_table_name], :foreign_key => [:foreign_key], :dependent => :destroy # The following is only setup once unless method_defined? :read_attribute_without_custom_field_behavior # Carry out delayed actions before save after_validation :save_modified_custom_field_attributes, :on => :update private alias_method_chain :read_attribute, :custom_field_behavior alias_method_chain :write_attribute, :custom_field_behavior end end end |