Skip to content

Commit

Permalink
Handle zero plural resource names
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwinger committed Apr 12, 2018
1 parent fa7a3ab commit de75144
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/controllers/alchemy/admin/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def create
resource_instance_variable.save
render_errors_or_redirect(
resource_instance_variable,
resources_path(resource_handler.namespaced_resources_name, search_filter_params),
resources_path(resource_instance_variable.class, search_filter_params),
flash_notice_for_resource_action
)
end
Expand All @@ -70,7 +70,7 @@ def update
resource_instance_variable.update_attributes(resource_params)
render_errors_or_redirect(
resource_instance_variable,
resources_path(resource_handler.namespaced_resources_name, search_filter_params),
resources_path(resource_instance_variable.class, search_filter_params),
flash_notice_for_resource_action
)
end
Expand Down
42 changes: 34 additions & 8 deletions spec/controllers/alchemy/admin/resources_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,47 @@

describe '#update' do
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: 'some_query'}, page: 6} }
let!(:peter) { create(:event, name: 'Peter') }

it 'redirects to index, keeping the current location parameters' do
post :update, params: {id: peter.id, event: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
context 'with regular noun model name' do
let(:peter) { create(:event, name: 'Peter') }

it 'redirects to index, keeping the current location parameters' do
post :update, params: {id: peter.id, event: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
end
end

context 'with zero plural noun model name' do
let!(:peter) { create(:series, name: 'Peter') }
let(:params) { {q: { name_cont: 'some_query'}, page: 6} }

it 'redirects to index, keeping the current location parameters' do
expect(controller).to receive(:controller_path) { 'admin/series' }
post :update, params: {id: peter.id, series: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/series?page=6&q%5Bname_cont%5D=some_query")
end
end
end

describe '#create' do
let(:params) { {q: {name_or_hidden_name_or_description_or_location_name_cont: 'some_query'}, page: 6} }
let!(:location) { create(:location) }

it 'redirects to index, keeping the current location parameters' do
post :create, params: {event: {name: "Hans", location_id: location.id}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
context 'with regular noun model name' do
it 'redirects to index, keeping the current location parameters' do
post :create, params: {event: {name: "Hans", location_id: location.id}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
end
end

context 'with zero plural noun model name' do
let(:params) { {q: {name_cont: 'some_query'}, page: 6} }

it 'redirects to index, keeping the current location parameters' do
expect(controller).to receive(:controller_path) { 'admin/series' }
post :create, params: {series: {name: "Hans"}}.merge(params)
expect(response.redirect_url).to eq("http://test.host/admin/series?page=6&q%5Bname_cont%5D=some_query")
end
end
end

Expand All @@ -76,4 +102,4 @@
expect(response.redirect_url).to eq("http://test.host/admin/events?page=6&q%5Bname_or_hidden_name_or_description_or_location_name_cont%5D=some_query")
end
end
end
end
2 changes: 2 additions & 0 deletions spec/dummy/app/controllers/admin/series_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Admin::SeriesController < Alchemy::Admin::ResourcesController
end
2 changes: 2 additions & 0 deletions spec/dummy/app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ def initialize(user)
can :index, :admin_events
can :manage, Location
can :index, :admin_locations
can :manage, Series
can :index, :admin_series
end
end
2 changes: 2 additions & 0 deletions spec/dummy/app/models/series.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Series < ActiveRecord::Base
end
4 changes: 4 additions & 0 deletions spec/dummy/config/initializers/alchemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
name: 'Locations',
controller: '/admin/locations',
action: 'index'
}, {
name: 'Series',
controller: '/admin/series',
action: 'index'
}]
}
)
1 change: 1 addition & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace :admin do
resources :events
resources :locations
resources :series
end

mount Alchemy::Engine => "/"
Expand Down
7 changes: 7 additions & 0 deletions spec/dummy/db/migrate/20180409171801_create_series.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateSeries < ActiveRecord::Migration[5.1]
def change
create_table :series do |t|
t.string :name
end
end
end
6 changes: 5 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180227224537) do
ActiveRecord::Schema.define(version: 20180409171801) do

create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -318,4 +318,8 @@
t.datetime "updated_at"
end

create_table "series", force: :cascade do |t|
t.string "name"
end

end
4 changes: 4 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
factory :location do
name 'Awesome Lodge'
end

factory :series do
name 'My Series'
end
end

0 comments on commit de75144

Please sign in to comment.