Skip to content

Commit

Permalink
Fixed issue "Can't create new tags on contact #317"
Browse files Browse the repository at this point in the history
  • Loading branch information
vairix-garbeletche committed Feb 28, 2014
1 parent 2fc5cb8 commit 27ab952
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/assets/javascripts/crm_select2.js.coffee
Expand Up @@ -13,6 +13,13 @@
$(".select2").each ->
$(this).select2 'width':'resolve'

$(".select2_tag").each ->
$(this).select2
'width':'resolve'
tags: $(this).data("tags")
placeholder: $(this).data("placeholder")
multiple: $(this).data("multiple")

$(document).ready ->
crm.make_select2()

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/entities_controller.rb
Expand Up @@ -81,7 +81,7 @@ def versions

#----------------------------------------------------------------------------
def field_group
if @tag = Tag.find_by_name(params[:tag].strip)
if @tag = Tag.find_or_create_by_name(params[:tag].strip)
if @field_group = FieldGroup.find_by_tag_id_and_klass_name(@tag.id, klass.to_s)
@asset = klass.find_by_id(params[:asset_id]) || klass.new
render 'fields/group' and return
Expand Down
10 changes: 4 additions & 6 deletions app/views/shared/_tags.html.haml
Expand Up @@ -3,9 +3,7 @@
%td{ :valign => :top, :colspan => span }
.label Tags:

= f.select :tag_list, Tag.all.map{|t| [t.name, t.name] },
{ value: f.object.tags.map{|t| t.name } },
{ :multiple => true, :class => 'select2',
'data-placeholder' => t(:select_or_create_tags),
'data-url' => url_for(action: 'field_group'),
'data-asset-id' => f.object.try(:id) }
= f.hidden_field :tag_list,
{:class => "select2_tag", :data => {:tags => Tag.all.map{|t| t.name},
:url => url_for(action: 'field_group'), :placeholder => t(:select_or_create_tags),
:multiple => true, :asset_id => f.object.try(:id) }}
37 changes: 37 additions & 0 deletions spec/controllers/entities/contacts_controller_spec.rb
Expand Up @@ -216,6 +216,43 @@
end
end

# GET /contacts/field_group AJAX
#----------------------------------------------------------------------------
describe "responding to GET field_group" do

context "with an existing tag" do
before :each do
@tag = FactoryGirl.create(:tag)
end

it "should return with an existing tag name" do
xhr :get, :field_group, {:tag => @tag.name}
assigns[:tag].name == @tag.name
end

it "should have the same count of tags" do
xhr :get, :field_group, {:tag => @tag.name}
Tag.count.should equal(1)
end

end

context "without an existing tag" do
it "should return a new Tag with a new name" do
tag_name = "New-Tag"
xhr :get, :field_group, {:tag => tag_name}
assigns[:tag].name == tag_name
end

it "should create a new Tag with a new name" do
tag_name = "New-Tag-1"
xhr :get, :field_group, {:tag => tag_name}
Tag.count.should equal(1)
end
end

end

# GET /contacts/1/edit AJAX
#----------------------------------------------------------------------------
describe "responding to GET edit" do
Expand Down

0 comments on commit 27ab952

Please sign in to comment.