Module: Mongoid::Noteable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid_noteable/version.rb,
lib/mongoid_noteable/noteable.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

define 3 methods



64
65
66
67
68
69
70
# File 'lib/mongoid_noteable/noteable.rb', line 64

def method_missing(name, *args)
  if name.to_s =~ /^news_with_(subject|action|object)$/i
    rebuild_news(self.news.by_field($1, args[0]))
  else
    super
  end
end

Instance Method Details

#add_news(*args) ⇒ Object

add one piece of news

Example: >> @u.add_news(“I”, “voted for”, “Bush”)

> nil

Arguments:

args => news objects


34
35
36
37
38
39
40
41
42
# File 'lib/mongoid_noteable/noteable.rb', line 34

def add_news(*args)
  self.news.create!(:subject => args[0].to_s, :action => args[1].to_s, :object => args[2].to_s, :headline => (args[3] == 1 ? true : false))

  if self.news.count > self.news_count
    self.news[self.news_count..-1].each do |news|
      news.destroy
    end
  end
end

#all_newsObject

get all news



46
47
48
# File 'lib/mongoid_noteable/noteable.rb', line 46

def all_news
  rebuild_news(self.news.desc_created_at)
end

#highlight_newsObject

highlighted news



52
53
54
# File 'lib/mongoid_noteable/noteable.rb', line 52

def highlight_news
  rebuild_news(self.news.by_field("headline", true))
end

#recent_news(time = 3600) ⇒ Object

get all recent news



58
59
60
# File 'lib/mongoid_noteable/noteable.rb', line 58

def recent_news(time = 3600)
  rebuild_news(self.news.by_time(time))
end

#set_news_limit(limit) ⇒ Object

set how many pieces of news one user can have

Example: >> @u = User.new >> @u.save >> @u.set_news_limit(1000)

> true

Arguments:

limit => Integer


21
22
23
# File 'lib/mongoid_noteable/noteable.rb', line 21

def set_news_limit(limit)
  self.update_attribute(:news_count, limit)
end