Skip to content

Commit c7028f9

Browse files
committed
Fix models and specs
1 parent 163e0d1 commit c7028f9

13 files changed

+67
-73
lines changed

app/controllers/opportunities_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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" }

app/controllers/protips_controller.rb

Lines changed: 9 additions & 9 deletions
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(CGI.unescape(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

Lines changed: 16 additions & 20 deletions
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_on :tags
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

Lines changed: 4 additions & 4 deletions
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_on :tags
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)

app/models/protip.rb

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Protip < ActiveRecord::Base
3939
user_ip: proc { self.user.last_ip },
4040
user_agent: proc { self.user.last_ua }
4141

42-
attr_taggable :topics, :users
42+
acts_as_taggable_on :topics, :users
4343
attr_accessor :upvotes
4444

4545
DEFAULT_IP_ADDRESS = '0.0.0.0'
@@ -73,7 +73,7 @@ class Protip < ActiveRecord::Base
7373
validates :title, presence: true, length: { minimum: 5, maximum: MAX_TITLE_LENGTH }
7474
validates :body, presence: true
7575
validates :kind, presence: true, inclusion: { in: KINDS }
76-
validates :topics, length: { minimum: 1 }
76+
validates :topic_list, length: { minimum: 1 }
7777

7878
after_validation :tag_user
7979
before_create :assign_random_id
@@ -345,7 +345,7 @@ def index_search_after_destroy
345345

346346

347347
def networks
348-
Network.tagged_with(self.topics)
348+
Network.tagged_with(self.topic_list)
349349
end
350350

351351
def orphan?
@@ -357,7 +357,7 @@ def update_network(event=:new_protip)
357357
end
358358

359359
def generate_event(options={})
360-
unless self.created_automagically? and self.topics.include?("github")
360+
unless self.created_automagically? and self.topic_list.include?("github")
361361
event_type = self.event_type(options)
362362
GenerateEventJob.perform_in(10.minutes, event_type, event_audience(event_type), self.to_event_hash(options), 1.minute)
363363
end
@@ -389,7 +389,7 @@ def event_audience(event_type)
389389
end
390390

391391
def slideshare?
392-
self.topics.count == 1 && self.topics.include?("slideshare")
392+
self.topics.count == 1 && self.topic_list.include?("slideshare")
393393
end
394394

395395
def event_type(options={})
@@ -403,7 +403,7 @@ def event_type(options={})
403403
end
404404

405405
def topic_ids
406-
topics_tags.pluck(:id)
406+
topics.pluck(:id)
407407
end
408408

409409
def to_indexed_json
@@ -459,7 +459,7 @@ def to_public_hash
459459
title: Sanitize.clean(title),
460460
body: body,
461461
html: Sanitize.clean(to_html),
462-
tags: topics,
462+
tags: topic_list,
463463
upvotes: upvotes,
464464
url: path,
465465
upvote_path: upvote_path,
@@ -518,7 +518,7 @@ def original?
518518
end
519519

520520
def tokenized_skills
521-
@tokenized_skills ||= self.topics.collect { |tag| Skill.tokenize(tag) }
521+
@tokenized_skills ||= self.topic_list.collect { |tag| Skill.tokenize(tag) }
522522
end
523523

524524
def to_param
@@ -844,7 +844,7 @@ def owned_by?(user)
844844
alias_method :owner?, :owned_by?
845845

846846
def tag_user
847-
self.users = [self.user.try(:username)] if self.users.blank?
847+
self.user_list = [self.user.try(:username)] if self.users.blank?
848848
end
849849

850850
def reassign_to(user)
@@ -853,32 +853,28 @@ def reassign_to(user)
853853
end
854854

855855
def tags
856-
topics + users
856+
topic_list + user_list
857857
end
858858

859859
def link
860860
self.links.first
861861
end
862862

863863
def reformat_tags!
864-
if self.topics.count == 1 && self.topics.first =~ /\s/
865-
self.topics = self.topics.first.split(/\s/)
864+
if self.topic_list.count == 1 && self.topic_list.first =~ /\s/
865+
self.topic_list = self.topic_list.first.split(/\s/)
866866
end
867867
end
868868

869869
def sanitize_tags!
870-
new_topics = self.topics.reject { |tag| tag.blank? }.map do |topic|
870+
new_topics = self.topic_list.reject { |tag| tag.blank? }.map do |topic|
871871
sanitized_topic = self.class.preprocess_tag(topic)
872872
invalid_topic = topic.match("^((?!#{VALID_TAG}).)*$") && $1
873873
errors[:topics] << "The tag '#{topic}' has invalid characters: #{invalid_topic unless invalid_topic.nil?}" if sanitized_topic.nil?
874874
sanitized_topic
875875
end
876876
new_topics = new_topics.compact.uniq
877-
self.topics = new_topics if topics.blank? or topics_changed?
878-
end
879-
880-
def topics_changed?
881-
self.topics_tags.map(&:name) != self.topics
877+
self.topic_list = new_topics if topic_list.blank? or topic_list_changed?
882878
end
883879

884880
def viewed_by(viewer)
@@ -954,7 +950,7 @@ def matching_jobs
954950
if self.user.team && self.user.team.hiring?
955951
self.user.team.best_positions_for(self.user)
956952
else
957-
Opportunity.based_on(self.topics)
953+
Opportunity.based_on(self.topic_list)
958954
end
959955
end
960956

@@ -973,7 +969,7 @@ def check_links
973969

974970
private
975971
def need_to_extract_data_from_links
976-
self.topics.blank? || self.title.blank?
972+
self.topic_list.blank? || self.title.blank?
977973
end
978974

979975
def adjust_like_value(user, like_value)

app/views/opportunities/_form.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
=j.text_area :description
1919

2020
%fieldset
21-
=j.label :tags, 'Primary skills the person will use. (comma separated)'
22-
=j.text_field :tags, value: params[:tags] || @job.tags.join(",")
21+
=j.label :tag_list, 'Primary skills the person will use. (comma separated)'
22+
=j.text_field :tag_list, value: params[:tag_list] || @job.tag_list.join(",")
2323

2424
%fieldset
2525
-if @team.locations.any?

app/views/protips/_new_or_edit.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
%li.full-list=link_to('How to write a great pro tip', 'https://coderwall.com/p/o42nvq', target: "_blank")
3232

3333
.rule.edit-tags
34-
= p.input :topics, placeholder: "Tags, comma separated", label: false, input_html: {class: "tags cf", value: @protip.topics.join(","), id: "protip_tags", :autocomplete=>'off'}
34+
= p.input :topic_list, placeholder: "Tags, comma separated", label: false, input_html: {class: "tags cf", value: @protip.topic_list.join(","), id: "protip_tags", :autocomplete=>'off'}
3535

3636
.x-tip-content.preview.back.side.cf#x-protip-preview
3737

app/views/protips/_protip.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
= protip.total_views
8585
views
8686
%ul#tags.cf{itemprop: :keywords}
87-
- protip.topics.each do |tag|
87+
- protip.topic_list.each do |tag|
8888
%li
8989
%a{ href: "/p/t/#{ tag.parameterize }" }
9090
= tag
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ActsAsTaggableOn.delimiter = [' ', ',']
2+
ActsAsTaggableOn.force_lowercase = true

0 commit comments

Comments
 (0)