Module: RedmineCrm::ActsAsTaggable::Taggable::SingletonMethods
- Defined in:
- lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb
Instance Method Summary collapse
-
#available_tags(options = {}) ⇒ Object
Return all avalible tags for a project or global Example: Question.available_tags(:project => @project_id ).
- #caching_tag_list? ⇒ Boolean
- #find_options_for_find_tagged_with(tags, options = {}) ⇒ Object
- #find_options_for_tag_counts(options = {}) ⇒ Object
-
#find_related_tags(tags, options = {}) ⇒ Object
Returns an array of related tags.
-
#find_tagged_with(*args) ⇒ Object
(also: #tagged_with)
Pass either a tag, string, or an array of strings or tags.
- #joins_for_match_all_tags(tags) ⇒ Object
-
#tag_counts(options = {}) ⇒ Object
(also: #all_tag_counts)
Calculate the tag counts for all tags.
Instance Method Details
#available_tags(options = {}) ⇒ Object
Return all avalible tags for a project or global Example: Question.available_tags(:project => @project_id )
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 92 def ( = {}) project = [:project] limit = [:limit].to_i.zero? ? 30 : [:limit].to_i scope = Tag.where({}) class_name = quote_string_value(base_class.name) join = [] join << "JOIN #{Tagging.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id " join << "JOIN #{table_name} ON #{table_name}.id = #{Tagging.table_name}.taggable_id AND #{Tagging.table_name}.taggable_type = #{class_name} " if attribute_names.include?('project_id') && project join << "JOIN #{Project.table_name} ON #{Project.table_name}.id = #{table_name}.project_id" scope = scope.where("#{table_name}.project_id = ?", project.id) end if [:name_like] scope = scope.where("LOWER(#{Tag.table_name}.name) LIKE LOWER(?)", "%#{[:name_like]}%") end group_fields = '' group_fields << ", #{Tag.table_name}.created_at" if Tag.respond_to?(:created_at) group_fields << ", #{Tag.table_name}.updated_at" if Tag.respond_to?(:updated_at) if base_class.respond_to?(:visible_condition) visible_condition = base_class.visible_condition(User.current) if visible_condition.include?('project_id') && join.all? { |jn| jn.exclude?('JOIN projects') } join << "JOIN #{Project.table_name} ON #{Project.table_name}.id = #{table_name}.project_id" end scope = scope.where(visible_condition) end scope = scope.joins(join.join(' ')) scope = scope.select("#{Tag.table_name}.*, COUNT(DISTINCT #{Tagging.table_name}.taggable_id) AS count") scope = scope.group("#{Tag.table_name}.id, #{Tag.table_name}.name #{group_fields}") scope = scope.having('COUNT(*) > 0') scope = scope.order("#{Tag.table_name}.name") scope = scope.limit(limit) scope end |
#caching_tag_list? ⇒ Boolean
259 260 261 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 259 def caching_tag_list? column_names.include?(cached_tag_list_column_name) end |
#find_options_for_find_tagged_with(tags, options = {}) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 166 def (, = {}) = .is_a?(Array) ? TagList.new(.map(&:to_s)) : TagList.from() = .dup return {} if .empty? conditions = [] conditions << sanitize_sql(.delete(:conditions)) if [:conditions] taggings_alias, = "#{table_name}_taggings", "#{table_name}_tags" joins = [ "INNER JOIN #{Tagging.table_name} #{taggings_alias} ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key} AND #{taggings_alias}.taggable_type = #{quote_string_value(base_class.name)}", "INNER JOIN #{Tag.table_name} #{} ON #{}.id = #{taggings_alias}.tag_id" ] if .delete(:exclude) conditions << <<-END #{table_name}.id NOT IN (SELECT #{Tagging.table_name}.taggable_id FROM #{Tagging.table_name} INNER JOIN #{Tag.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id WHERE #{()} AND #{Tagging.table_name}.taggable_type = #{quote_string_value(base_class.name)}) END else if .delete(:match_all) joins << () else conditions << (, ) end end { :select => "DISTINCT #{table_name}.* ", :joins => joins.join(" "), :conditions => conditions.join(" AND ") }.reverse_merge!() end |
#find_options_for_tag_counts(options = {}) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 235 def ( = {}) = .dup scope = scope_attributes # scope(:find) conditions = [] conditions << send(:sanitize_sql_for_assignment, .delete(:conditions)) if [:conditions] conditions << send(:sanitize_sql_for_assignment, scope) if scope conditions << "#{Tagging.table_name}.taggable_type = #{quote_string_value(base_class.name)}" conditions << type_condition unless descends_from_active_record? conditions.delete('') conditions.compact! conditions = conditions.join(" AND ") joins = ["INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{Tagging.table_name}.taggable_id"] joins << .delete(:joins) if [:joins].present? # joins << scope[:joins] if scope && scope[:joins].present? joins = joins.join(" ") = { :conditions => conditions, :joins => joins }.update() Tag.() end |
#find_related_tags(tags, options = {}) ⇒ Object
Returns an array of related tags. Related tags are all the other tags that are found on the models tagged with the provided tags.
Pass either a tag, string, or an array of strings or tags.
Options:
:order - SQL Order how to order the tags. Defaults to "count DESC, tags.name".
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 136 def (, = {}) = .is_a?(Array) ? TagList.new(.map(&:to_s)) : TagList.from() = find_tagged_with() return [] if .blank? = .map{|c| c.id }.join(",") Tag.select( "#{Tag.table_name}.*, COUNT(#{Tag.table_name}.id) AS count").joins( "JOIN #{Tagging.table_name} ON #{Tagging.table_name}.taggable_type = '#{base_class.name}' AND #{Tagging.table_name}.taggable_id IN (#{}) AND #{Tagging.table_name}.tag_id = #{Tag.table_name}.id").order( [:order] || "count DESC, #{Tag.table_name}.name").group( "#{Tag.table_name}.id, #{Tag.table_name}.name HAVING #{Tag.table_name}.name NOT IN (#{.map { |n| quote_string_value(n) }.join(",")})") end |
#find_tagged_with(*args) ⇒ Object Also known as: tagged_with
Pass either a tag, string, or an array of strings or tags.
Options:
:exclude - Find models that are not tagged with the given tags
:match_all - Find models that match all of the given tags, not just one
:conditions - A piece of SQL conditions to add to the query
159 160 161 162 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 159 def find_tagged_with(*args) = (*args) .blank? ? [] : select([:select]).where([:conditions]).joins([:joins]).order([:order]).to_a end |
#joins_for_match_all_tags(tags) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 203 def () joins = [] .each_with_index do |tag, index| taggings_alias, = "taggings_#{index}", "tags_#{index}" join = <<-END INNER JOIN #{Tagging.table_name} #{taggings_alias} ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key} AND #{taggings_alias}.taggable_type = #{quote_string_value(base_class.name)} INNER JOIN #{Tag.table_name} #{} ON #{taggings_alias}.tag_id = #{}.id AND #{}.name = ? END joins << sanitize_sql([join, tag]) end joins.join(' ') end |
#tag_counts(options = {}) ⇒ Object Also known as: all_tag_counts
Calculate the tag counts for all tags.
See Tag.counts for available options.
228 229 230 231 232 |
# File 'lib/redmine_crm/acts_as_taggable/rcrm_acts_as_taggable.rb', line 228 def tag_counts( = {}) # Tag.find(:all, find_options_for_tag_counts(options)) opt = () Tag.select(opt[:select]).where(opt[:conditions]).joins(opt[:joins]).group(opt[:group]).having(opt[:having]).order(opt[:order]).limit([:limit]) end |