Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
more specs; add tag to search results feature in place
Browse files Browse the repository at this point in the history
  • Loading branch information
robmckinnon committed Sep 23, 2008
1 parent 57bb7f6 commit c952e11
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/controllers/records_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ def rename_tag
end
end

def add_tag
if is_admin? && request.post? && !params['ids'].blank? && !params['tag'].blank?
tag = decode_tag(params['tag'])
ids = params['ids'].split(',').collect{|i| i.to_i}

records = Record.add_tag(tag, ids)

flash[:notice] = "Adding '#{tag}' tag successful, #{records.size} records changed."
redirect_to :action => 'search', :query => params['query']
else
render :text => ''
end
end

def delete_tag
if is_admin? && request.delete? && !params['id'].blank?
tag = decode_tag(params['id'])
Expand Down
10 changes: 10 additions & 0 deletions app/models/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def find_with_status status
statuses.sort_by(&:name)
end

def add_tag tag, record_ids
records = record_ids.collect{|id| Record.find(id)}

records.each do |record|
record.add_tag tag
record.save
end
records
end

def rename_tag old_tag, new_tag
records = find_with_tag old_tag
records.each do |record|
Expand Down
8 changes: 8 additions & 0 deletions app/views/records/search_results.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
- @title = 'Search results'

- unless @records.blank?
%p= flash[:notice]
- if @is_admin
%p
- form_tag :action => 'add_tag', :method => :post do
= submit_tag "Add tag to search results:"
= text_field_tag 'tag'
= hidden_field_tag 'query', @term
= hidden_field_tag 'ids', @records.collect(&:id).sort.join(',')
- pagination_links = will_paginate @records
= pagination_links
= render :partial => 'search_result', :collection => @records
Expand Down
2 changes: 1 addition & 1 deletion app/views/records/tag_results.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- @title = "Tag: #{@tag}"

- if !@records.blank?
%p= flash[:notice]
- if @is_admin
%fieldset.tags-edit
%legend Edit tags
Expand All @@ -12,7 +13,6 @@
= text_field_tag 'new_tag'
= hidden_field_tag 'old_tag', @tag

%p= flash[:notice]
= render :partial => 'search_result', :collection => @records
- elsif flash[:notice]
%p= flash[:notice]
Expand Down
11 changes: 11 additions & 0 deletions spec/models/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
end
end

describe 'when asked to add tag to records' do
it 'should add tag to each identified record' do
tag = 'tag'
id = 1
Record.should_receive(:find).with(id).and_return @record
@record.should_receive(:add_tag).with(tag)
@record.should_receive(:save)
Record.add_tag(tag, [id]).should == [@record]
end
end

describe 'when asked to delete tag' do
it 'should delete tag from each record tagged with that tag' do
tag = 'tag'
Expand Down

0 comments on commit c952e11

Please sign in to comment.