Module: ActiveRecord::Has::Features::ClassMethods
- Defined in:
- lib/has_features/active_record/has/features.rb
Overview
This has extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a featured_position column defined as an integer on the mapped database table.
Todo list example:
class TodoList < ActiveRecord::Base
has_many :todo_items, :order => "featured_position"
end
class Author < ActiveRecord::Base
belongs_to :todo_list
has_features :scope => :organization
end
.featured = true
Instance Method Summary collapse
-
#has_features(options = {}) ⇒ Object
Configuration options are:.
Instance Method Details
#has_features(options = {}) ⇒ Object
Configuration options are:
-
column- specifies the column name to use for keeping the featured_position integer (default:featured_position) -
scope- restricts what is to be considered a list. Given a symbol, it’ll attach_id(if it hasn’t already been added) and use that as the foreign key restriction. It’s also possible to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. Example:has_features :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'
32 33 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 |
# File 'lib/has_features/active_record/has/features.rb', line 32 def has_features( = {}) configuration = { :column => "featured_position", :scope => "1 = 1" } configuration.update() if .is_a?(Hash) configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/ if configuration[:scope].is_a?(Symbol) scope_condition_method = %( def scope_condition self.class.send(:sanitize_sql_hash_for_conditions, { :#{configuration[:scope].to_s} => send(:#{configuration[:scope].to_s}) }) end ) elsif configuration[:scope].is_a?(Array) scope_condition_method = %( def scope_condition attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column| memo[column.intern] = send(column.intern); memo end self.class.send(:sanitize_sql_hash_for_conditions, attrs) end ) else scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end" end class_eval " include ActiveRecord::Has::Features::InstanceMethods\n \n def featured_position_column\n '\#{configuration[:column]}'\n end\n \n def has_features_class\n ::\#{self.name}\n end\n \n scope :featured, order(\"\#{configuration[:column]} asc\").where(\"\#{configuration[:column]} is not null\")\n\n \#{scope_condition_method}\n \n before_destroy :decrement_positions_on_lower_featured_items\n EOV\nend\n" |