Skip to content

Commit

Permalink
Add settings form
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Feb 17, 2018
1 parent d2ba9e8 commit f11d7e1
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 4 deletions.
2 changes: 2 additions & 0 deletions apps/admin/config/routes.rb
Expand Up @@ -5,3 +5,5 @@
# get '/hello', to: ->(env) { [200, {}, ['Hello from Hanami!']] }
get '/', to: 'projects#index', as: :projects
get '/settings', to: 'settings#index', as: :settings
get '/settings/new', to: 'settings#new', as: :new_settings
post '/settings/create', to: 'settings#create', as: :create_settings
21 changes: 21 additions & 0 deletions apps/admin/controllers/settings/create.rb
@@ -0,0 +1,21 @@
module Admin::Controllers::Settings
class Create
include Admin::Action

params do
required(:setting).schema do
required(:title).filled(:str?)
end
end

def call(params)
if params.valid?
SettingRepository.new.create(params[:setting])

redirect_to routes.settings_path
else
self.status = 422
end
end
end
end
11 changes: 11 additions & 0 deletions apps/admin/controllers/settings/new.rb
@@ -0,0 +1,11 @@
module Admin::Controllers::Settings
class New
include Admin::Action

expose :settings

def call(params)
@settings = SettingRepository.new.for_display
end
end
end
2 changes: 2 additions & 0 deletions apps/admin/templates/settings/index.html.slim
Expand Up @@ -8,3 +8,5 @@ table.table.table-hover
tr
td = key
td = value

.new-settings = link_to 'New Settings', routes.new_settings_path
3 changes: 3 additions & 0 deletions apps/admin/templates/settings/new.html.slim
@@ -0,0 +1,3 @@
h2 New Settings

= form(settings)
14 changes: 14 additions & 0 deletions apps/admin/views/settings/new.rb
@@ -0,0 +1,14 @@
module Admin::Views::Settings
class New
include Admin::View

def form(settings)
form_for :setting, routes.create_settings_path do
label :title
text_field :title, value: settings.title

submit 'Create'
end
end
end
end
2 changes: 1 addition & 1 deletion lib/contributors/repositories/setting_repository.rb
@@ -1,6 +1,6 @@
class SettingRepository < Hanami::Repository

def for_display
settings.select(:title).first
settings.select(:title).last
end
end
30 changes: 30 additions & 0 deletions spec/admin/controllers/settings/create_spec.rb
@@ -0,0 +1,30 @@
RSpec.describe Admin::Controllers::Settings::Create, type: :action do
let(:action) { described_class.new }
let(:settings_repo) { SettingRepository.new }

after { settings_repo.clear }


describe '#call' do
context 'correct params' do
let(:params) { Hash[setting: {title: 'hello'}] }

it 'creates a new setting record' do
expect{action.call(params)}.to change{settings_repo.all.count}.by(1)
end
end

context 'incorrect params' do
let(:params) { Hash[] }

it 'does not creates a new setting record' do
expect{action.call(params)}.to_not change{settings_repo.all.count}
end

it 'returns 422' do
response = action.call(params)
expect(response[0]).to eq 422
end
end
end
end
2 changes: 1 addition & 1 deletion spec/admin/controllers/settings/index_spec.rb
Expand Up @@ -22,7 +22,7 @@

after { settings_repo.clear }

it { expect(action.settings).to be_a(Setting) }
it { expect(action.settings).to eq({title: 'Hanami'}) }
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/contributors/repositories/setting_repository_spec.rb
Expand Up @@ -10,8 +10,7 @@
end

context 'returns settings with columns to display' do
it { expect(repo.for_display).to be_a Setting }
it { expect(repo.for_display.to_hash).to eq ({title: 'Hanami'}) }
it { expect(repo.for_display).to eq({title: 'Hanami'}) }
end
end
end

0 comments on commit f11d7e1

Please sign in to comment.