Class: Category
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Category
- Includes:
- CommunityRelated
- Defined in:
- /var/apps/qpixel/app/models/category.rb
Constant Summary collapse
- COLORS =
['turquoise', 'green', 'blue', 'darkblue', 'purple', 'gray', 'bluegray', 'yellow', 'orange', 'pink', 'red'].freeze
Class Method Summary collapse
-
.accessible_to(user) ⇒ ActiveRecord::Relation<Category>
Gets categories appropriately scoped for a given user.
-
.by_id(id) ⇒ Object
@todo: Do we need this method?.
-
.by_lowercase_name(name) ⇒ Category?
Gets category matching a given name.
-
.search(term) ⇒ ActiveRecord::Relation<Category>
Gets a collection of categories matching a given search term.
Instance Method Summary collapse
-
#homepage? ⇒ Boolean
Is the category set as the homepage?.
-
#new_posts_for?(user) ⇒ Boolean
Are there new posts for a given user since their last visit?.
-
#public? ⇒ Boolean
Can anyone view the category (even if not logged in)?.
- #top_level_post_types ⇒ Object
- #update_activity(last_activity) ⇒ Object
Methods inherited from ApplicationRecord
#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules
Class Method Details
.accessible_to(user) ⇒ ActiveRecord::Relation<Category>
Gets categories appropriately scoped for a given user
69 70 71 72 73 74 75 76 |
# File '/var/apps/qpixel/app/models/category.rb', line 69 def self.accessible_to(user) if user&.at_least_moderator? return Category.all end trust_level = user&.trust_level || 0 Category.where('min_view_trust_level <= ?', trust_level) end |
.by_id(id) ⇒ Object
@todo: Do we need this method?
89 90 91 92 93 94 |
# File '/var/apps/qpixel/app/models/category.rb', line 89 def self.by_id(id) categories = Rails.cache.fetch 'categories/by_id' do Category.all.to_h { |c| [c.id, c] } end categories[id] end |
.by_lowercase_name(name) ⇒ Category?
Gets category matching a given name
81 82 83 84 85 86 |
# File '/var/apps/qpixel/app/models/category.rb', line 81 def self.by_lowercase_name(name) categories = Rails.cache.fetch 'categories/by_lowercase_name' do Category.all.to_h { |c| [c.name.downcase, c.id] } end Category.find_by(id: categories[name]) end |
.search(term) ⇒ ActiveRecord::Relation<Category>
Gets a collection of categories matching a given search term
99 100 101 |
# File '/var/apps/qpixel/app/models/category.rb', line 99 def self.search(term) where('name LIKE ?', "%#{sanitize_sql_like(term)}%") end |
Instance Method Details
#homepage? ⇒ Boolean
Is the category set as the homepage?
32 33 34 |
# File '/var/apps/qpixel/app/models/category.rb', line 32 def homepage? is_homepage == true end |
#new_posts_for?(user) ⇒ Boolean
Are there new posts for a given user since their last visit?
50 51 52 53 54 55 56 57 58 59 60 |
# File '/var/apps/qpixel/app/models/category.rb', line 50 def new_posts_for?(user) key = "#{community_id}/#{user.id}/#{id}/last_visit" Rails.cache.fetch key, expires_in: 5.minutes do Rack::MiniProfiler.step "Redis: category last visit (#{key})" do activity_key = "#{community_id}/#{id}/last_activity" last_visit = RequestContext.redis.get(key) last_activity = RequestContext.redis.get(activity_key) || DateTime.parse last_visit.nil? || last_activity > DateTime.parse(last_visit) end end end |
#public? ⇒ Boolean
Can anyone view the category (even if not logged in)?
38 39 40 41 |
# File '/var/apps/qpixel/app/models/category.rb', line 38 def public? trust_level = min_view_trust_level || -1 trust_level <= 0 end |
#top_level_post_types ⇒ Object
43 44 45 |
# File '/var/apps/qpixel/app/models/category.rb', line 43 def top_level_post_types post_types.where(is_top_level: true) end |
#update_activity(last_activity) ⇒ Object
62 63 64 |
# File '/var/apps/qpixel/app/models/category.rb', line 62 def update_activity(last_activity) RequestContext.redis.set("#{community_id}/#{id}/last_activity", last_activity) end |