Sha256: 1f1bebbf09a391ca2a246941f06fded176c83804ce8282fc9745c80532a2b7cf

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

class Content < ActiveRecord::Base

  before_save :adjust_positions
  
  
  class << self; attr_accessor :tipes; end
  
  # Put all the different kinds of content you will have in here to get a select helper and some named scopes 
  # generated for you. 
  @tipes = [
  # "Billboard",
  # "What You Learn",
  # "Tips",
  # "Questions Copy",
  # "FAQ",
  # "Examples",
  # "Testimonial",
  # "Navigation",
  ]
  
  # Pretty much always want to only find content of one tipe at a time, so generate scopes for all tipes.
  @tipes.each do |thing|
    scope "for_#{thing.downcase.gsub(' ', '_')}".intern, where("tipe = ?", thing)
  end

  # It's pretty common to want the ability to hide some content.  Use the visible scope liberally.
  scope :visible, lambda {|x| where(:visible => x)}

  # Returns all Content with like tipe.
  def brothers
    Content.where("tipe = ?", self.tipe).where("id NOT in (#{self.id})")
  end

  private
    #  It's a pain with ordered content to have to manually update each piece of content when rearranging them.  
    #  adjust_positions will make sure when you change the position of one content, all other content of that tipe
    #  has position changed to keep a reasonable order.  
    def adjust_positions
      return true unless changed.include?('position')  
      return true unless brothers.map{|x| x.position}.include?(self.position)
      
      brothers.where("position >= ?", self.position).update_all("position = (position + 1)")
    end
    
    
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
contentment-0.8.4 lib/generators/contentment/templates/content.rb
contentment-0.8.3 lib/generators/contentment/templates/content.rb
contentment-0.7.1 lib/generators/contentment/templates/content.rb
contentment-0.7.0 lib/generators/contentment/templates/content.rb
contentment-0.6.1 lib/generators/contentment/templates/content.rb
contentment-0.6.0 lib/generators/contentment/templates/content.rb