Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjlandau committed Mar 14, 2014
1 parent c738890 commit 3f8b902
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
13 changes: 10 additions & 3 deletions lib/active_admin_associations/association_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ module AssociationActions
def association_actions
member_action :unrelate, :method => :put do
reflection = resource_class.reflect_on_association(params[:relationship_name].to_sym)

if reflection.collection?
related_record = reflection.klass.find(params[:related_id])
resource.send(params[:relationship_name]).delete(related_record)
else
resource.update_attribute("#{params[:relationship_name]}_id", nil)
resource.update_attribute(reflection.foreign_key, nil)
end

flash[:notice] = "The recored has been unrelated."

redirect_to request.headers["Referer"].presence || admin_dashboard_url
end

member_action :relate, :method => :put do
reflection = resource_class.reflect_on_association(params[:relationship_name].to_sym)

if reflection.collection?
record_to_relate = reflection.klass.find(params[:related_id])
resource.send(params[:relationship_name]) << record_to_relate
else
resource.update_attribute("#{params[:relationship_name]}_id", record_to_relate)
resource.update_attribute(reflection.foreign_key, params[:related_id])
end

flash[:notice] = "The recored has been related."

redirect_to request.headers["Referer"].presence || admin_dashboard_url
end

Expand All @@ -30,6 +36,7 @@ def association_actions
association_config = active_admin_config.form_associations[relationship_name]
relationship_class = resource_class.reflect_on_association(relationship_name).klass
association_columns = association_config.fields.presence || relationship_class.content_columns

render :partial => 'admin/shared/collection_table', :locals => {
:object => resource,
:collection => resource.send(relationship_name).page(params[:page]),
Expand All @@ -40,4 +47,4 @@ def association_actions
end
end
end
end
end
9 changes: 3 additions & 6 deletions lib/active_admin_associations/autocompleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ def _autocomplete_format_result(record)
def _format_autocomplete_label(record)
if autocomplete_options[:format_label].present?
if autocomplete_options[:format_label].is_a?(Symbol)
record.send(autocomplete_options[:format_label])
return record.send(autocomplete_options[:format_label])
elsif autocomplete_options[:format_label].respond_to?(:call)
autocomplete_options[:format_label].call(record)
else
record.send(autocomplete_attribute)
return autocomplete_options[:format_label].call(record)
end
else
record.send(autocomplete_attribute)
end
record.send(autocomplete_attribute)
end

def configured_autocomplete_result_formatter?
Expand Down
2 changes: 1 addition & 1 deletion spec/autocompleter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
end
end

context 'with a custom label formatter that is a method on the class' do
context 'with a custom label formatter that is a proc' do
before do
Tag.autocomplete_options[:format_label] = proc{|record|
"#{record.name}: Tag"
Expand Down
32 changes: 30 additions & 2 deletions spec/controllers/admin_posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
let!(:post){ Factory(:post) }
let!(:tag1){ Factory(:tag) }
let!(:tag2){ Factory(:tag) }
let!(:user){ Factory(:user) }

before do
admin_login_as
post.tags << tag1
end

describe 'relate a resource' do
describe 'relate a tag' do
before do
@initial_tagging_count = Tagging.count
put :relate, :id => post.id, :relationship_name => "tags",
Expand All @@ -28,8 +29,22 @@
post.tags(true).should include(tag2)
end
end

describe 'relate a creator' do
before do
put :relate, :id => post.id, :relationship_name => "creator",
:related_id => user.id
end

it { should respond_with(:redirect) }
it { should set_the_flash.to('The recored has been related.') }

it 'properly relates the record' do
post.reload.creator(true).should == user
end
end

describe 'unrelate a resource' do
describe 'unrelate a tag' do
before do
@initial_tagging_count = Tagging.count
put :unrelate, :id => post.id, :relationship_name => "tags",
Expand All @@ -47,6 +62,19 @@
post.tags(true).should_not include(tag1)
end
end

describe 'unrelate a creator' do
before do
put :unrelate, :id => post.id, :relationship_name => "creator"
end

it { should respond_with(:redirect) }
it { should set_the_flash.to('The recored has been unrelated.') }

it "removes the creator" do
post.reload.creator(true).should be_nil
end
end

describe 'page related' do
before do
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/autocomplete_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
end

describe 'get #index with autocomplete_query_term_param_names config setting set' do
before do
Rails.application.config.activeadmin_associations.autocomplete_query_term_param_names = [:q, :term]
get :index, :model => 'tag', :q => "Mus", :format => 'json'
end
after do
Rails.application.config.activeadmin_associations.autocomplete_query_term_param_names = nil
end

it { should respond_with(:success) }
it { should respond_with_content_type(:json) }
end

describe 'get #index with no value' do
before do
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Post < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings

validates_presence_of :title, :body, :creator_id
validates_presence_of :title, :body

autocomplete :title
end

0 comments on commit 3f8b902

Please sign in to comment.