diff --git a/app/controllers/lists_controller.rb b/app/controllers/lists_controller.rb index 1d7c5739c..cb48bc175 100644 --- a/app/controllers/lists_controller.rb +++ b/app/controllers/lists_controller.rb @@ -9,13 +9,14 @@ class ListsController < ApplicationController # POST /lists #---------------------------------------------------------------------------- def create - list_params[:user_id] = (current_user.id if params[:is_global].to_i.zero?) + list_attr = list_params.to_h + list_attr["user_id"] = current_user.id if params["is_global"] != "1" # Find any existing list with the same name (case insensitive) - if @list = List.where("lower(name) = ?", list_params[:name].downcase).where(user_id: list_params[:user_id]).first - @list.update(list_params) + if @list = List.where("lower(name) = ?", list_attr[:name].downcase).where(user_id: list_attr[:user_id]).first + @list.update(list_attr) else - @list = List.create(list_params) + @list = List.create(list_attr) end respond_with(@list) diff --git a/app/models/list.rb b/app/models/list.rb index a51eb8093..4a54ea4b5 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -8,7 +8,7 @@ class List < ActiveRecord::Base validates_presence_of :name validates_presence_of :url - belongs_to :user + belongs_to :user, optional: true # Parses the controller from the url def controller diff --git a/spec/controllers/lists_controller_spec.rb b/spec/controllers/lists_controller_spec.rb index 94baede2f..8affd9ba2 100644 --- a/spec/controllers/lists_controller_spec.rb +++ b/spec/controllers/lists_controller_spec.rb @@ -8,4 +8,43 @@ require 'spec_helper' describe ListsController do + before(:each) do + login + end + + let(:list_url) { "/contacts?q%5Bs%5D%5B0%5D%5Bname%5D=&q%5Bs%5D%5B0%5D%5Bdir%5D=asc&q%5Bg%5D%5B0%5D%5Bm%5D=and&q%5Bg%5D%5B0%5D%5Bc%5D%5B0%5D%5Ba%5D%5B0%5D%5Bname%5D=first_name&q%5Bg%5D%5B0%5D%5Bc%5D%5B0%5D%5Bp%5D=cont&q%5Bg%5D%5B0%5D%5Bc%5D%5B0%5D%5Bv%5D%5B0%5D%5Bvalue%5D=test&distinct=1&page=1" } + + describe "global list items" do + let(:list_name) { "Global list item" } + let(:is_global) { "1" } + it "creating should be successful" do + post :create, params: { list: { name: list_name, url: list_url }, is_global: is_global }, xhr: true + expect(assigns(:list).persisted?).to eql(true) + expect(response).to render_template("lists/create") + end + it "updating should be successful" do + @list = List.create!(name: list_name, url: "/test") + post :create, params: { list: { name: list_name, url: list_url }, is_global: is_global }, xhr: true + expect(assigns(:list).persisted?).to eql(true) + expect(@list.reload.url).to eql(list_url) + expect(response).to render_template("lists/create") + end + it "delete list item" do + @list = List.create!(name: list_name, url: "/test") + delete :destroy, params: { id: @list.id }, xhr: true + expect { List.find(@list.id) }.to raise_error(ActiveRecord::RecordNotFound) + expect(response).to render_template("lists/destroy") + end + end + + describe "personal list items" do + let(:list_name) { "Personal list item" } + let(:is_global) { "0" } + + it "creating should be successful" do + post :create, params: { list: { name: list_name, url: list_url }, is_global: is_global }, xhr: true + expect(assigns(:list).persisted?).to eql(true) + expect(response).to render_template("lists/create") + end + end end