Module: ActiveRecord::Acts::MuckContent::ClassMethods

Defined in:
lib/active_record/acts/muck_content.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_muck_content(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record/acts/muck_content.rb', line 12

def acts_as_muck_content(options = {})
  
  default_options = { 
    :sanitize_content => true,
    :enable_auto_translations => true,
    :enable_solr => false,
    :enable_comments => false
  }
  options = default_options.merge(options)
  
  acts_as_nested_set :scope => [:contentable_id, :contentable_type]
  acts_as_taggable
  acts_as_commentable if options[:enable_comments]
            
  validates_presence_of :title 
  validates_presence_of :body_raw
  validates_presence_of :locale
  
  belongs_to :contentable, :polymorphic => true
  belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
  has_many   :content_permissions, :dependent => :destroy
  has_many   :content_translations, :dependent => :destroy

  named_scope :by_newest, :order => "created_at DESC"
  named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
  named_scope :by_alpha, :order => "title ASC"
  named_scope :public, :conditions => "is_public = true"
  named_scope :by_parent, lambda { |parent_id| { :conditions => ['parent_id = ?', parent_id || 0] } }
  named_scope :by_creator, lambda { |creator_id| { :conditions => ['creator_id = ?', creator_id || 0] } }
  named_scope :no_contentable, :conditions => 'contentable_id IS NULL'
  # include the '/' in case the user forgets.  Note 'get_content_scope' will add the '/' so 
  # if we don't include it here the content won't be found as the scope won't match up. 
  named_scope :by_scope, lambda { |scope| { :conditions => ["slugs.scope = ?", File.join('/', scope)], :include => [:slugs] } } 
  
  has_friendly_id :title, :use_slug => true, :scope => :get_content_scope

  if options[:sanitize_content]
    before_save :sanitize_attributes
  end
  
  before_save :ensure_locale_is_string
  
  # TODO add states - draft, published
  # maybe move content body_raw -> body_draft -> body
  # then you could be working on a revision without it being live

  # TODO add versions
  # if options[:git_repository]
  #   versioning(:title) do |version|
  #     version.repository = options[:git_repository]
  #     version.message = lambda { |content| "Committed by #{content.author.name}" }
  #   end
  # end

  if options[:enable_auto_translations]
    after_save :auto_translate
  end

  if options[:enable_solr]
    require 'acts_as_solr'
    acts_as_solr({ :fields => [ :search_content => 'string' ] }, { :multi_core => true, :default_core => 'en' })
  end

  class_eval <<-EOV
    attr_accessor :uri_path
    attr_accessor :custom_scope
    
    # prevents a user from submitting a crafted form that bypasses activation
    attr_protected :created_at, :updated_at, :creator, :body
  EOV

  include ActiveRecord::Acts::MuckContent::InstanceMethods
  extend ActiveRecord::Acts::MuckContent::SingletonMethods
  
end