Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saving search list #1305

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/controllers/lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions spec/controllers/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Dismissed Show dismissed Hide dismissed
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

Check notice

Code scanning / Rubocop

Prefer Ruby 1.9 hash syntax { a: 1, b: 2 } over 1.8 syntax { :a => 1, :b => 2 }. Note test

Style/HashSyntax: Omit the hash value.
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
Dismissed Show dismissed Hide dismissed
expect(assigns(:list).persisted?).to eql(true)
expect(response).to render_template("lists/create")
end
end
end
Loading