Class: TagGroup
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- TagGroup
- Defined in:
- app/models/tag_group.rb
Instance Attribute Summary collapse
-
#permissions ⇒ Object
Returns the value of attribute permissions.
Class Method Summary collapse
-
.find_id_by_slug(slug) ⇒ Object
TODO: long term we can cache this if TONs of tag groups exist.
- .resolve_permissions(permissions) ⇒ Object
- .visible(guardian) ⇒ Object
Instance Method Summary collapse
- #apply_permissions ⇒ Object
- #init_permissions ⇒ Object
- #parent_tag_name=(tag_names_arg) ⇒ Object
- #remove_parent_from_group ⇒ Object
- #tag_names=(tag_names_arg) ⇒ Object
Instance Attribute Details
#permissions ⇒ Object
Returns the value of attribute permissions.
26 27 28 |
# File 'app/models/tag_group.rb', line 26 def @permissions end |
Class Method Details
.find_id_by_slug(slug) ⇒ Object
TODO: long term we can cache this if TONs of tag groups exist
51 52 53 54 |
# File 'app/models/tag_group.rb', line 51 def self.find_id_by_slug(slug) self.pluck(:id, :name).each { |id, name| return id if Slug.for(name) == slug } nil end |
.resolve_permissions(permissions) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'app/models/tag_group.rb', line 56 def self.() .map do |group, | group_id = Group.group_id_from_param(group) = TagGroupPermission.[.to_sym] unless .is_a?( Integer, ) [group_id, ] end end |
.visible(guardian) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/models/tag_group.rb', line 89 def self.visible(guardian) if guardian.is_staff? TagGroup else # ( # tag group is restricted to a category you can see # OR # tag group is not restricted to any categories # ) # AND tag group can be seen by everyone filter_sql = <<~SQL ( id IN (SELECT tag_group_id FROM category_tag_groups WHERE category_id IN (?)) OR id NOT IN (SELECT tag_group_id FROM category_tag_groups) ) AND id IN (SELECT tag_group_id FROM tag_group_permissions WHERE group_id IN (?)) SQL TagGroup.where( filter_sql, guardian.allowed_category_ids, DiscourseTagging.permitted_group_ids(guardian), ) end end |
Instance Method Details
#apply_permissions ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'app/models/tag_group.rb', line 75 def if @permissions .destroy_all @permissions.each do |group_id, | .build(group_id: group_id, permission_type: ) end @permissions = nil end end |
#init_permissions ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/models/tag_group.rb', line 66 def unless .present? || @permissions .build( group_id: Group::AUTO_GROUPS[:everyone], permission_type: TagGroupPermission.[:full], ) end end |
#parent_tag_name=(tag_names_arg) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/tag_group.rb', line 32 def parent_tag_name=(tag_names_arg) if tag_names_arg.empty? self.parent_tag = nil else if tag_name = DiscourseTagging.( tag_names_arg, Guardian.new(Discourse.system_user), ).first self.parent_tag = Tag.find_by_name(tag_name) || Tag.create(name: tag_name) end end end |
#remove_parent_from_group ⇒ Object
85 86 87 |
# File 'app/models/tag_group.rb', line 85 def remove_parent_from_group .delete(parent_tag) if .include?(parent_tag) end |
#tag_names=(tag_names_arg) ⇒ Object
28 29 30 |
# File 'app/models/tag_group.rb', line 28 def tag_names=(tag_names_arg) DiscourseTagging.(self, tag_names_arg, unlimited: true) end |