Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user.rb

Constant Summary collapse

USERNAME_MIN_LENGTH =
4
USERNAME_MAX_LENGTH =
20
PASSWORD_MIN_LENGTH =
4
PASSWORD_MAX_LENGTH =
40
EMAIL_MAX_LENGTH =
50
USERNAME_RANGE =
USERNAME_MIN_LENGTH..USERNAME_MAX_LENGTH
PASSWORD_RANGE =
PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH
MAX_NUMBER_OF_ARTICLES_PER_NEWSUPDATE =

configuration settings

10
MAX_SIZE_OF_A_MAIL_IN_CHARACTERS =
20000
NEWSUPDATE_RECENT_NUMBER_OF_DAYS =
120
QUERY_AUGMENT_PARTIAL =
''
@@per_page =
20

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.post_recent_news_for_every_userObject



69
70
71
72
73
74
75
# File 'app/models/user.rb', line 69

def self.post_recent_news_for_every_user
  logger.info 'Starting to send the recent news for every user through email.'
  User.all.each do |user|
    user.post_recent_news
  end
  logger.info 'Sending recent news for every user finished.'
end

Instance Method Details

#can_be_modified_by(user) ⇒ Object



127
128
129
# File 'app/models/user.rb', line 127

def can_be_modified_by(user)
  !user.nil? and (user==self or user.has_role?(:admin))
end

#collect_recent_articles(&article_condition) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/user.rb', line 77

def collect_recent_articles(&article_condition)
  Sunspot.commit_if_dirty
=begin
  searchquery = search_keywords.map do |search_keyword|
    "(#{search_keyword.query})"
  end.join(' OR ')
  search = Sunspot.search(Article) do
    keywords searchquery
    with(:published_at).greater_than Time.now - NEWSUPDATE_RECENT_NUMBER_OF_DAYS.day
    dynamic :status do
      without Article.give_user_status_symbol(self, ArticleStatus::STATUS_VIEWED), true
      without Article.give_user_status_symbol(self, ArticleStatus::STATUS_SENT), true
    end
    order_by(:published_at, :desc)
    paginate(:page => 1, :per_page => MAX_NUMBER_OF_ARTICLES_PER_NEWSUPDATE)
  end
  search.results
=end
  set_all_articles = Set.new
  search_keywords.each do |search_keyword|
    article_update = recent_articles_for_search_keyword(search_keyword)
    set_all_articles += article_update
  end
  arr_all_articles = set_all_articles.to_a
  arr_all_articles = arr_all_articles.find_all do |article|
    article_condition.call(article)
  end if article_condition
  arr_all_articles.sort! { |article1, article2| article2.published_at <=> article1.published_at }
  arr_all_articles = arr_all_articles[0, MAX_NUMBER_OF_ARTICLES_PER_NEWSUPDATE]
  arr_all_articles
end

#post_recent_newsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/user.rb', line 55

def post_recent_news
  recent_articles = collect_recent_articles do |article|
    article.size <= User::MAX_SIZE_OF_A_MAIL_IN_CHARACTERS
  end
  recent_articles.each do |article|
    UserMailer.deliver_newsupdate(self, article)
    article.set_user_status(self, ArticleStatus::STATUS_SENT)
    logger.info <<-EOF
    Email notification about article #{article.title} was sent to #{self.username},
    address #{self.email}
    EOF
  end
end

#recent_articles_for_search_keyword(search_keyword) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/user.rb', line 109

def recent_articles_for_search_keyword(search_keyword)
  searchquery = search_keyword.query
  self_user = self
  search = Sunspot.search(Article) do
    keywords "#{searchquery} #{QUERY_AUGMENT_PARTIAL}"
    with(:published_at).greater_than Time.now - NEWSUPDATE_RECENT_NUMBER_OF_DAYS.day
    dynamic :status do
      without Article.give_user_status_symbol(self_user, ArticleStatus::STATUS_VIEWED), true
      without Article.give_user_status_symbol(self_user, ArticleStatus::STATUS_SENT), true
    end
    order_by(:published_at, :desc)
    paginate(:page => 1, :per_page => MAX_NUMBER_OF_ARTICLES_PER_NEWSUPDATE)
  end
  #FIXME: This is for debugging purposes, trying to catch a hardly reproducably bug, which I dont understand why happens
  STDERR << search.results.to_a.to_s
  search.results.to_a
end