Class: Region
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Region
- Includes:
- NestedPriority, Toggleable
- Defined in:
- app/models/region.rb
Overview
Region
Attributes:
children_cache [Array<integer>]
country_id [Country], optional
created_at [DateTime]
data [json]
header_image [SimpleImageUploader], optional
latitude [float]
locative [string], optional
long_slug [string]
longitude [float]
map_geometry [text], optional
name [string]
parent_id [Region], optional
parents_cache [string]
posts_count [integer]
short_name [string], optional
slug [string]
svg_geometry [text], optional
users_count [integer]
visible [boolean]
Class Method Summary collapse
- .entity_parameters ⇒ Object
- .siblings(item) ⇒ Object
- .synchronization_parameters ⇒ Object
-
.update_post_count(old_id, new_id) ⇒ Object
Post moved to other region.
Instance Method Summary collapse
- #add_user(user) ⇒ Object
- #branch_ids ⇒ Array<Integer>
- #branch_name ⇒ Object
- #cache_children!(new_cache = []) ⇒ Object
- #cache_parents! ⇒ Object
- #depth ⇒ Object
- #editable_by?(user) ⇒ Boolean
- #long_name ⇒ Object
- #parent_ids ⇒ Object
- #parents ⇒ Object
- #remove_user(user) ⇒ Object
- #subbranch_ids ⇒ Array<Integer>
Class Method Details
.entity_parameters ⇒ Object
51 52 53 |
# File 'app/models/region.rb', line 51 def self.entity_parameters %i[header_image priority visible] end |
.siblings(item) ⇒ Object
47 48 49 |
# File 'app/models/region.rb', line 47 def self.siblings(item) where(country_id: item&.country_id, parent_id: item&.parent_id) end |
.synchronization_parameters ⇒ Object
55 56 57 58 59 |
# File 'app/models/region.rb', line 55 def self.synchronization_parameters ignored = %w[header_image users_count] column_names.reject { |c| ignored.include?(c) } end |
.update_post_count(old_id, new_id) ⇒ Object
Post moved to other region
Decrements posts_count for region with old_id and increments for new_id
67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/region.rb', line 67 def self.update_post_count(old_id, new_id) part = 'update regions set posts_count = posts_count' unless new_id.nil? region = find(new_id) connection.execute("#{part} + 1 where id in (#{region.branch_ids.join(',')})") end return if old_id.nil? region = find(old_id) connection.execute("#{part} - 1 where id in (#{region.branch_ids.join(',')})") end |
Instance Method Details
#add_user(user) ⇒ Object
141 142 143 |
# File 'app/models/region.rb', line 141 def add_user(user) RegionUser.create(region: self, user: user) end |
#branch_ids ⇒ Array<Integer>
95 96 97 |
# File 'app/models/region.rb', line 95 def branch_ids parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id] end |
#branch_name ⇒ Object
114 115 116 117 118 |
# File 'app/models/region.rb', line 114 def branch_name return short_name if parents.blank? "#{parents.map(&:short_name).join('/')}/#{short_name}" end |
#cache_children!(new_cache = []) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/region.rb', line 128 def cache_children!(new_cache = []) if new_cache.blank? new_cache = child_regions.order('id asc').pluck(:id, :children_cache) end self.children_cache += [new_cache.flatten] self.children_cache.uniq! save! parent&.cache_children!([id] + children_cache) end |
#cache_parents! ⇒ Object
120 121 122 123 124 125 |
# File 'app/models/region.rb', line 120 def cache_parents! return if parent.nil? self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '') save! end |
#depth ⇒ Object
104 105 106 |
# File 'app/models/region.rb', line 104 def depth parent_ids.count end |
#editable_by?(user) ⇒ Boolean
80 81 82 |
# File 'app/models/region.rb', line 80 def editable_by?(user) Biovision::Components::RegionsComponent.allow?(user, 'manager') end |
#long_name ⇒ Object
108 109 110 111 112 |
# File 'app/models/region.rb', line 108 def long_name return name if parents.blank? "#{parents.map(&:name).join('/')}/#{name}" end |
#parent_ids ⇒ Object
90 91 92 |
# File 'app/models/region.rb', line 90 def parent_ids parents_cache.split(',').compact end |
#parents ⇒ Object
84 85 86 87 88 |
# File 'app/models/region.rb', line 84 def parents return [] if parents_cache.blank? Region.where(id: parent_ids).order('id asc') end |
#remove_user(user) ⇒ Object
146 147 148 |
# File 'app/models/region.rb', line 146 def remove_user(user) RegionUser.where(region: self, user: user).delete_all end |
#subbranch_ids ⇒ Array<Integer>
100 101 102 |
# File 'app/models/region.rb', line 100 def subbranch_ids [id] + children_cache end |