Module: Opinio::OpinioModel::ClassMethods

Defined in:
lib/opinio/opinio_model.rb

Instance Method Summary collapse

Instance Method Details

#opinio(*args) ⇒ Object

Adds the Opinio functionallity to the model You can pass a hash of options to customize the Opinio model

Options

:belongs_to

You can specify the class that owns the comments on config/initializers/opinio.rb but you can also pass it explicitly here: eg. :belongs_to => "Admin"

:counter_cache

Customize the counter cache here, defaults to false

:body_length

You can pass a Range to determine the size of the body that will be validated

:title_length

If you are using titles in your opinio model (set on the initializer) you can pass a Range so it is validated.



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
# File 'lib/opinio/opinio_model.rb', line 28

def opinio(*args)
  return if self.included_modules.include?(Opinio::OpinioModel::InstanceMethods)
  options = args.extract_options!

  if Opinio.use_title
    attr_accessible :title 
    validates       :title,
                    {:presence => true}.merge( :length => options[:title_length] )
  end
  attr_accessible :body

  commentable_options = { :polymorphic => true }
  if options[:counter_cache]
    commentable_options.merge!(:counter_cache => options[:counter_cache])
  end

  belongs_to :commentable, commentable_options
  belongs_to :owner, :class_name => options.reverse_merge(:belongs_to => Opinio.owner_class_name)[:belongs_to]

  #TODO: refactor this
  if Opinio.interval_between_comments
    validate :last_comment_time, :if => :new_record?
    cattr_accessor :comments_interval
    self.comments_interval = options.reverse_merge(:time => Opinio.interval_between_comments)[:time]
  end

  extra_options = {}
  if options[:body_length]
    extra_options = { :length => options[:body_length] }
  end

  validates :body,
            { :presence => true }.reverse_merge(extra_options)

  validates_presence_of :commentable

  scope :owned_by, lambda {|owner| where('owner_id = ?', owner.id) }

  send :include, Opinio::OpinioModel::InstanceMethods

  if Opinio.accept_replies
    send :include, RepliesSupport
  end

end