Skip to content

Commit 92e5d6a

Browse files
committed
Merge pull request coderwall#279 from rohitpaulk/replace-rocket-tag-with-aato
Replace rocket tag with acts_as_taggable_on [#480]
2 parents 75f242e + ac2cb88 commit 92e5d6a

18 files changed

+130
-115
lines changed

Gemfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ gem 'foreman'
9494
# Better logging
9595
gem 'awesome_print'
9696

97+
# Tagging
98+
gem 'acts-as-taggable-on', '~> 3.4'
99+
97100
gem 'faraday', '~> 0.8.1'
98101
gem 'metamagic'
99102

@@ -135,9 +138,6 @@ gem 'mongoid'
135138
gem 'mongo'
136139
gem 'mongoid_taggable'
137140
gem 'bson_ext'
138-
#Tagging
139-
gem 'rocket_tag'
140-
gem 'squeel', '1.0.1'
141141
gem 'strong_parameters'
142142
gem 'postgres_ext'
143143
# ElasticSearch client

Gemfile.lock

+3-11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ GEM
8181
activesupport (3.2.21)
8282
i18n (~> 0.6, >= 0.6.4)
8383
multi_json (~> 1.0)
84+
acts-as-taggable-on (3.4.2)
85+
activerecord (>= 3.2, < 5)
8486
acts_as_commentable (2.0.1)
8587
acts_as_follower (0.1.1)
8688
addressable (2.3.6)
@@ -475,8 +477,6 @@ GEM
475477
cliver (~> 0.3.1)
476478
multi_json (~> 1.0)
477479
websocket-driver (>= 0.2.0)
478-
polyamorous (0.5.0)
479-
activerecord (~> 3.0)
480480
polyglot (0.3.5)
481481
poro_plus (1.0.2)
482482
posix-spawn (0.3.9)
@@ -593,9 +593,6 @@ GEM
593593
rest-client (1.7.2)
594594
mime-types (>= 1.16, < 3.0)
595595
netrc (~> 0.7)
596-
rocket_tag (0.5.6)
597-
activerecord (>= 3.2.0)
598-
squeel (~> 1.0.0)
599596
rspec (3.1.0)
600597
rspec-core (~> 3.1.0)
601598
rspec-expectations (~> 3.1.0)
@@ -688,10 +685,6 @@ GEM
688685
multi_json (~> 1.0)
689686
rack (~> 1.0)
690687
tilt (~> 1.1, != 1.3.0)
691-
squeel (1.0.1)
692-
activerecord (~> 3.0)
693-
activesupport (~> 3.0)
694-
polyamorous (~> 0.5.0)
695688
strong_parameters (0.2.3)
696689
actionpack (~> 3.0)
697690
activemodel (~> 3.0)
@@ -763,6 +756,7 @@ PLATFORMS
763756
ruby
764757

765758
DEPENDENCIES
759+
acts-as-taggable-on (~> 3.4)
766760
acts_as_commentable (= 2.0.1)
767761
acts_as_follower (= 0.1.1)
768762
airbrake
@@ -855,7 +849,6 @@ DEPENDENCIES
855849
redcarpet
856850
redis-rails (~> 3.2)
857851
rest-client
858-
rocket_tag
859852
rspec-rails
860853
rubocop
861854
ruby-progressbar
@@ -872,7 +865,6 @@ DEPENDENCIES
872865
slim-rails
873866
spring
874867
spring-commands-rspec
875-
squeel (= 1.0.1)
876868
stripe!
877869
stripe-ruby-mock!
878870
strong_parameters

app/controllers/networks_controller.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def members
4141

4242
def show
4343
@protips = []
44-
@topics = @network.tags
44+
@topics = @network.tag_list
4545

4646
if (params[:sort].blank? && params[:filter].blank?) || params[:sort] == 'upvotes'
4747
@protips = @network.most_upvoted_protips(@per_page, @page)
@@ -141,7 +141,7 @@ def destroy
141141

142142
def add_tag
143143
tag = params[:tag]
144-
@network.tags << tag
144+
@network.tag_list.add(tag)
145145

146146
respond_to do |format|
147147
if @network.save
@@ -155,8 +155,8 @@ def add_tag
155155
end
156156

157157
def remove_tag
158-
tag = params[:tag]
159-
@network.tags = @network.tags.delete(tag)
158+
tag = params[:tag]
159+
@network.tag_list.remove(tag)
160160

161161
respond_to do |format|
162162
if @network.save
@@ -171,7 +171,7 @@ def remove_tag
171171

172172
def update_tags
173173
tags = params[:tags][:tags]
174-
@network.tags = tags.split(',').map(&:strip).select { |tag| Tag.exists?(name: tag) }
174+
@network.tag_list = tags.split(',').map(&:strip).select { |tag| Tag.exists?(name: tag) }
175175

176176
respond_to do |format|
177177
if @network.save

app/controllers/opportunities_controller.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class OpportunitiesController < ApplicationController
22
before_action :lookup_team, only: [:activate, :deactivate, :new, :create, :edit, :update, :visit]
33
before_action :lookup_opportunity, only: [:edit, :update, :activate, :deactivate, :visit]
4-
before_action :cleanup_params_to_prevent_rocket_tag_error
4+
before_action :cleanup_params_to_prevent_tagging_error
55
before_action :validate_permissions, only: [:new, :edit, :create, :update, :activate, :deactivate]
66
before_action :verify_payment, only: [:new, :create]
77
before_action :stringify_location, only: [:create, :update]
@@ -29,7 +29,7 @@ def edit
2929
end
3030

3131
def create
32-
opportunity_create_params = params.require(:opportunity).permit(:name, :team_id, :opportunity_type, :description, :tags, :location, :link, :salary, :apply, :remote)
32+
opportunity_create_params = params.require(:opportunity).permit(:name, :team_id, :opportunity_type, :description, :tag_list, :location, :link, :salary, :apply, :remote)
3333
@job = Opportunity.new(opportunity_create_params)
3434
respond_to do |format|
3535
if @job.save
@@ -42,7 +42,7 @@ def create
4242
end
4343

4444
def update
45-
opportunity_update_params = params.require(:opportunity).permit(:id, :name, :team_id, :opportunity_type, :description, :tags, :location, :link, :salary, :apply)
45+
opportunity_update_params = params.require(:opportunity).permit(:id, :name, :team_id, :opportunity_type, :description, :tag_list, :location, :link, :salary, :apply)
4646
respond_to do |format|
4747
if @job.update_attributes(opportunity_update_params)
4848
format.html { redirect_to teamname_path(@team.slug), notice: "#{@job.name} updated" }
@@ -131,10 +131,10 @@ def header_ok
131131
end
132132
end
133133

134-
def cleanup_params_to_prevent_rocket_tag_error
135-
if params[:opportunity] && params[:opportunity][:tags]
136-
params[:opportunity][:tags] = "#{params[:opportunity][:tags]}".split(',').map(&:strip).reject(&:empty?).join(",")
137-
params[:opportunity][:tags] = nil if params[:opportunity][:tags].strip.blank?
134+
def cleanup_params_to_prevent_tagging_error
135+
if params[:opportunity] && params[:opportunity][:tag_list]
136+
params[:opportunity][:tag_list] = "#{params[:opportunity][:tag_list]}".split(',').map(&:strip).reject(&:empty?).join(",")
137+
params[:opportunity][:tag_list] = nil if params[:opportunity][:tag_list].strip.blank?
138138
end
139139
end
140140

@@ -151,7 +151,7 @@ def all_job_locations
151151
end
152152

153153
def all_job_skills
154-
Rails.cache.fetch('job_skills', expires_in: 23.hours) { Opportunity.all.flat_map(&:tags).uniq.compact }
154+
Rails.cache.fetch('job_skills', expires_in: 23.hours) { Opportunity.all.flat_map(&:tag_list).uniq.compact }
155155
end
156156

157157
def closest_to_user(user)

app/controllers/protips_controller.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ def random
152152
end
153153
154154
def new
155-
new_params = params.permit(:topics)
155+
new_params = params.permit(:topic_list)
156156
157-
prefilled_topics = (new_params[:topics] || '').split('+').collect(&:strip)
158-
@protip = Protip.new(topics: prefilled_topics)
157+
prefilled_topics = (new_params[:topic_list] || '').split('+').collect(&:strip)
158+
@protip = Protip.new(topic_list: prefilled_topics)
159159
respond_with @protip
160160
end
161161
@@ -165,7 +165,7 @@ def edit
165165
166166
def create
167167
create_params = if params[:protip] && params[:protip].keys.present?
168-
params.require(:protip).permit(:title, :body, :user_id, topics: [])
168+
params.require(:protip).permit(:title, :body, :user_id, :topic_list)
169169
else
170170
{}
171171
end
@@ -187,7 +187,7 @@ def create
187187
def update
188188
# strong_parameters will intentionally fail if a key is present but has an empty hash. :(
189189
update_params = if params[:protip] && params[:protip].keys.present?
190-
params.require(:protip).permit(:title, :body, :user_id, topics: [])
190+
params.require(:protip).permit(:title, :body, :user_id, :topic_list)
191191
else
192192
{}
193193
end
@@ -233,8 +233,8 @@ def upvote
233233
end
234234
235235
def tag
236-
tag_params = params.permit(:topics)
237-
@protip.topics << tag_params[:topics] unless tag_params[:topics].nil?
236+
tag_params = params.permit(:topic_list)
237+
@protip.topic_list.add(tag_params[:topic_list]) unless tag_params[:topic_list].nil?
238238
end
239239
240240
def subscribe
@@ -316,7 +316,7 @@ def feature
316316
end
317317
318318
def delete_tag
319-
@protip.topics.delete(CGI.unescape(params.permit(:topic)))
319+
@protip.topic_list.remove(params.permit(:topic))
320320
respond_to do |format|
321321
if @protip.save
322322
format.html { redirect_to protip_path(@protip) }
@@ -349,7 +349,7 @@ def by_tags
349349
def preview
350350
preview_params = params.require(:protip).permit(:title, :body)
351351
352-
preview_params.delete(:topics) if preview_params[:topics].blank?
352+
preview_params.delete(:topic_list) if preview_params[:topic_list].blank?
353353
protip = Protip.new(preview_params)
354354
protip.updated_at = protip.created_at = Time.now
355355
protip.user = current_user

app/models/network.rb

+16-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Network < ActiveRecord::Base
1717
profile_url: { type: 'string', index: 'not_analyzed' },
1818
} } } }
1919

20-
attr_taggable :tags
20+
acts_as_taggable
2121
acts_as_followable
2222
attr_accessor :resident_expert
2323
has_many :network_experts, autosave: true, dependent: :destroy
@@ -61,7 +61,7 @@ def top_tags_not_networks
6161
end
6262

6363
def top_tags_not_in_any_networks
64-
top_tags.where('tags.name NOT IN (?)', Network.all.map(&:tags).flatten)
64+
top_tags.where('tags.name NOT IN (?)', Network.all.map(&:tag_list).flatten)
6565
end
6666

6767
def top_tags
@@ -82,21 +82,17 @@ def create_slug!
8282
end
8383

8484
def tag_with_name!
85-
unless self.tags.include? self.name
86-
self.tags = (self.tags + [self.name, self.slug])
85+
unless self.tag_list.include? self.name
86+
self.tag_list = (self.tag_list + [self.name, self.slug])
8787
end
8888
end
8989

9090
def correct_tags
91-
if self.tags_changed?
92-
self.tags = self.tags.uniq.select { |tag| Tag.exists?(name: tag) }.reject { |tag| (tag != self.name) && Network.exists?(name: tag) }
91+
if self.tag_list_changed?
92+
self.tags_list = self.tag_list.uniq.select { |tag| Tag.exists?(name: tag) }.reject { |tag| (tag != self.name) && Network.exists?(name: tag) }
9393
end
9494
end
9595

96-
def tags_changed?
97-
self.tags_tags.map(&:name) != self.tags
98-
end
99-
10096
def protips_tags_with_count
10197
self.protips.joins("inner join taggings on taggings.taggable_id = protips.id").joins('inner join tags on taggings.tag_id = tags.id').where("taggings.taggable_type = 'Protip' AND taggings.context = 'topics'").select('tags.name, count(tags.name)').group('tags.name').order('count(tags.name) DESC')
10298
end
@@ -117,7 +113,7 @@ def assign_mayor!
117113

118114
candidate = self.in_line_to_the_throne.first
119115
unless candidate.nil?
120-
Rails.logger.debug "finding a mayor among: #{self.tags}" if ENV['DEBUG']
116+
Rails.logger.debug "finding a mayor among: #{self.tag_list}" if ENV['DEBUG']
121117
person_with_most_upvoted_protips_on_topic = User.find(candidate.user_id)
122118
Rails.logger.debug "mayor for #{name} found: #{person_with_most_upvoted_protips_on_topic.username}" if ENV['DEBUG']
123119

@@ -163,41 +159,41 @@ def to_public_hash
163159
end
164160

165161
def protips
166-
@protips ||= Protip.tagged_with(self.tags, on: :topics)
162+
@protips ||= Protip.tagged_with(self.tag_list, on: :topics)
167163
end
168164

169165
def upvotes
170166
self.protips.joins("inner join likes on likes.likable_id = protips.id").where("likes.likable_type = 'Protip'").select('count(*)').count
171167
end
172168

173169
def most_upvoted_protips(limit = nil, offset = 0)
174-
Protip.search_trending_by_topic_tags("sort:upvotes desc", self.tags, offset, limit)
170+
Protip.search_trending_by_topic_tags("sort:upvotes desc", self.tag_list, offset, limit)
175171
end
176172

177173
def new_protips(limit = nil, offset = 0)
178-
Protip.search("sort:created_at desc", self.tags, page: offset, per_page: limit)
174+
Protip.search("sort:created_at desc", self.tag_list, page: offset, per_page: limit)
179175
end
180176

181177
def featured_protips(limit = nil, offset = 0)
182178
#self.protips.where(:featured => true)
183-
Protip.search("featured:true", self.tags, page: offset, per_page: limit)
179+
Protip.search("featured:true", self.tag_list, page: offset, per_page: limit)
184180

185181
end
186182

187183
def flagged_protips(limit = nil, offset = 0)
188-
Protip.search("flagged:true", self.tags, page: offset, per_page: limit)
184+
Protip.search("flagged:true", self.tag_list, page: offset, per_page: limit)
189185
end
190186

191187
def highest_scored_protips(limit=nil, offset =0, field=:trending_score)
192-
Protip.search("sort:#{field} desc", self.tags, page: offset, per_page: limit)
188+
Protip.search("sort:#{field} desc", self.tag_list, page: offset, per_page: limit)
193189
end
194190

195191
def mayor_protips(limit=nil, offset =0)
196-
Protip.search_trending_by_user(self.mayor.username, nil, self.tags, offset, limit)
192+
Protip.search_trending_by_user(self.mayor.username, nil, self.tag_list, offset, limit)
197193
end
198194

199195
def expert_protips(limit=nil, offset =0)
200-
Protip.search_trending_by_user(self.resident_expert.username, nil, self.tags, offset, limit)
196+
Protip.search_trending_by_user(self.resident_expert.username, nil, self.tag_list, offset, limit)
201197
end
202198

203199
def members(limit = -1, offset = 0)
@@ -226,7 +222,7 @@ def resident_expert_from_env
226222
end
227223

228224
def assign_members
229-
Skill.where(name: self.tags).select('DISTINCT(user_id)').map(&:user).each do |member|
225+
Skill.where(name: self.tag_list).select('DISTINCT(user_id)').map(&:user).each do |member|
230226
member.join(self)
231227
end
232228
end

app/models/opportunity.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Opportunity < ActiveRecord::Base
66
include SearchModule
77
include OpportunityMapping
88

9-
attr_taggable :tags
9+
acts_as_taggable
1010

1111
OPPORTUNITY_TYPES = %w(full-time part-time contract internship)
1212

@@ -16,7 +16,7 @@ class Opportunity < ActiveRecord::Base
1616
validates :name, presence: true, allow_blank: false
1717
validates :opportunity_type, inclusion: { in: OPPORTUNITY_TYPES }
1818
validates :description, length: { minimum: 100, maximum: 2000 }
19-
validates :tags, with: :tags_within_length
19+
validates :tag_list, with: :tags_within_length
2020
validates :location, presence: true, allow_blank: false
2121
validates :location_city, presence: true, allow_blank: false, unless: lambda { location && anywhere?(location) }
2222
validates :salary, presence: true, numericality: true, inclusion: 0..800_000, allow_blank: true
@@ -76,13 +76,13 @@ def self.random
7676
end
7777

7878
def tags_within_length
79-
tags_string = tags.join(',')
79+
tags_string = tag_list.join(',')
8080
errors.add(:skill_tags, 'are too long(Maximum is 250 characters)') if tags_string.length > 250
8181
errors.add(:base, 'You need to specify at least one skill tag') if tags_string.length == 0
8282
end
8383

8484
def update_cached_tags
85-
self.cached_tags = tags.join(',')
85+
self.cached_tags = tag_list.join(',')
8686
end
8787

8888
def seize_by(user)

0 commit comments

Comments
 (0)