Module: ActiveRecord::Has::Features::InstanceMethods

Defined in:
lib/has_features/active_record/has/features.rb

Overview

All the methods available to a record that has had has_features specified. Each method works by assuming the object to be the item in the list, so chapter.move_lower would move that chapter lower in the list of all chapters. Likewise, chapter.first? would return true if that chapter is the first in the list of all chapters.

Instance Method Summary collapse

Instance Method Details

#decrement_positionObject

Decrease the position of this item without adjusting the rest of the list.



142
143
144
145
# File 'lib/has_features/active_record/has/features.rb', line 142

def decrement_position
  return unless in_list?
  update_attribute featured_position_column, self.send(featured_position_column).to_i - 1
end

#featureObject



194
195
196
# File 'lib/has_features/active_record/has/features.rb', line 194

def feature
  add_to_list_bottom
end

#feature_at(position = 1) ⇒ Object

Insert the item at the given position (defaults to the top position of 1).



83
84
85
# File 'lib/has_features/active_record/has/features.rb', line 83

def feature_at(position = 1)
  feature_at_position(position)
end

#featured=(val) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/has_features/active_record/has/features.rb', line 180

def featured=(val)
  if true == val || "true" == val
    add_to_list_bottom
    save
  else
    unfeature
  end
end

#featured?Boolean Also known as: featured

Returns:

  • (Boolean)


189
190
191
# File 'lib/has_features/active_record/has/features.rb', line 189

def featured?
  !self.send(featured_position_column).nil?
end

#first?Boolean

Return true if this object is the first in the list.

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/has_features/active_record/has/features.rb', line 148

def first?
  return false unless in_list?
  self.send(featured_position_column) == 1
end

#higher_itemObject

Return the next higher item in the list.



160
161
162
163
164
165
# File 'lib/has_features/active_record/has/features.rb', line 160

def higher_item
  return nil unless in_list?
  has_features_class.find(:first, :conditions =>
    "#{scope_condition} AND #{featured_position_column} = #{(send(featured_position_column).to_i - 1).to_s}"
  )
end

#in_list?Boolean

Test if this record is in a list

Returns:

  • (Boolean)


176
177
178
# File 'lib/has_features/active_record/has/features.rb', line 176

def in_list?
  !send(featured_position_column).nil?
end

#increment_positionObject

Increase the position of this item without adjusting the rest of the list.



136
137
138
139
# File 'lib/has_features/active_record/has/features.rb', line 136

def increment_position
  return unless in_list?
  update_attribute featured_position_column, self.send(featured_position_column).to_i + 1
end

#last?Boolean

Return true if this object is the last in the list.

Returns:

  • (Boolean)


154
155
156
157
# File 'lib/has_features/active_record/has/features.rb', line 154

def last?
  return false unless in_list?
  self.send(featured_position_column) == bottom_position_in_list
end

#lower_itemObject

Return the next lower item in the list.



168
169
170
171
172
173
# File 'lib/has_features/active_record/has/features.rb', line 168

def lower_item
  return nil unless in_list?
  has_features_class.find(:first, :conditions =>
    "#{scope_condition} AND #{featured_position_column} = #{(send(featured_position_column).to_i + 1).to_s}"
  )
end

#move_higherObject

Swap positions with the next higher item, if one exists.



98
99
100
101
102
103
104
105
# File 'lib/has_features/active_record/has/features.rb', line 98

def move_higher
  return unless higher_item

  has_features_class.transaction do
    higher_item.increment_position
    decrement_position
  end
end

#move_lowerObject

Swap positions with the next lower item, if one exists.



88
89
90
91
92
93
94
95
# File 'lib/has_features/active_record/has/features.rb', line 88

def move_lower
  return unless lower_item

  has_features_class.transaction do
    lower_item.decrement_position
    increment_position
  end
end

#move_to_bottomObject

Move to the bottom of the list. If the item is already in the list, the items below it have their position adjusted accordingly.



109
110
111
112
113
114
115
# File 'lib/has_features/active_record/has/features.rb', line 109

def move_to_bottom
  return unless in_list?
  has_features_class.transaction do
    decrement_positions_on_lower_featured_items
    assume_bottom_position
  end
end

#move_to_topObject

Move to the top of the list. If the item is already in the list, the items above it have their position adjusted accordingly.



119
120
121
122
123
124
125
# File 'lib/has_features/active_record/has/features.rb', line 119

def move_to_top
  return unless in_list?
  has_features_class.transaction do
    increment_positions_on_higher_items
    assume_top_position
  end
end

#unfeatureObject

Removes the item from the list.



128
129
130
131
132
133
# File 'lib/has_features/active_record/has/features.rb', line 128

def unfeature
  if in_list?
    decrement_positions_on_lower_featured_items
    update_attribute featured_position_column, nil
  end
end