Sha256: 81832f408afada2c2a5fd160a37e35df435d1600e744f5f921487f7d9e3be551
Contents?: true
Size: 1.42 KB
Versions: 2
Compression:
Stored size: 1.42 KB
Contents
module ActiveRecord module EavHashes def self.included (base) base.extend ActiveRecord::EavHashes::ClassMethods end module ClassMethods def eav_hash_for (hash_name, options={}) # Fill in default options not otherwise specified options[:hash_name] = hash_name options[:parent_class_name] = self.name.to_sym options = ActiveRecord::EavHashes::Util::fill_options_hash options # Store the options hash in a class variable to create the EavHash object # if and when it's actually used. class_variable_set "@@#{hash_name}_hash_options".to_sym, options # Create the association, the entry update hook, and a helper method to lazy-load the entries class_eval <<-END_EVAL has_many :#{options[:entry_assoc_name]}, class_name: #{options[:entry_class_name]}, foreign_key: "#{options[:parent_assoc_name]}_id", dependent: :delete_all after_save :save_#{hash_name} def #{hash_name} @#{hash_name} ||= ActiveRecord::EavHashes::EavHash.new(self, @@#{hash_name}_hash_options) end def save_#{hash_name} @#{hash_name}.save_entries if @#{hash_name} end def self.find_by_#{hash_name} (key, value=nil) self.find (ActiveRecord::EavHashes::Util::run_find_expression(key, value, @@#{hash_name}_hash_options)) end END_EVAL end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
eav_hashes-1.0.3 | lib/eav_hashes/activerecord_extension.rb |
eav_hashes-1.0.2 | lib/eav_hashes/activerecord_extension.rb |