Skip to content

Commit

Permalink
Merge branch 'add-new-project-from-admin' of github.com:Bounga/contri…
Browse files Browse the repository at this point in the history
…butors into add-new-project-from-admin
  • Loading branch information
Bounga committed Apr 2, 2018
2 parents 9e1a3f0 + 131cc66 commit 8355656
Show file tree
Hide file tree
Showing 32 changed files with 319 additions and 25 deletions.
1 change: 1 addition & 0 deletions apps/admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# get '/hello', to: ->(env) { [200, {}, ['Hello from Hanami!']] }
root to: 'projects#index'
resources :projects, only: [:index, :new, :create]
resources :settings, only: [:index, :new, :create]
21 changes: 21 additions & 0 deletions apps/admin/controllers/settings/create.rb
Original file line number Diff line number Diff line change
@@ -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/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Admin::Controllers::Settings
class Index
include Admin::Action

expose :setting_history

def call(params)
@setting_history = SettingRepository.new.history
end
end
end
11 changes: 11 additions & 0 deletions apps/admin/controllers/settings/new.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Admin::Controllers::Settings
class New
include Admin::Action

expose :settings

def call(params)
@settings = SettingRepository.new.latest
end
end
end
2 changes: 2 additions & 0 deletions apps/admin/templates/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ html
= stylesheet 'bootstrap', 'main'
body
.container
= render partial: 'shared/navbar'

- if flash[:message]
.alert.alert-success
strong= flash[:message]
Expand Down
12 changes: 12 additions & 0 deletions apps/admin/templates/settings/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
h2 Setting History

table.table.table-hover
tr
th Title
th Changed at
- setting_history.each do |setting|
tr
td = setting.title
td = setting.created_at

.new-settings = link_to 'New Settings', routes.new_setting_path
3 changes: 3 additions & 0 deletions apps/admin/templates/settings/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2 New Settings

= form(settings)
8 changes: 8 additions & 0 deletions apps/admin/templates/shared/_navbar.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.admin-pages
ul.nav.nav-pills
li role="presentation"
a href='/admin' Repositories
li role="presentation"
a href='/admin/settings' Settings
li role="presentation"
a href='/' target='_blank' Main app
5 changes: 5 additions & 0 deletions apps/admin/views/settings/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Admin::Views::Settings
class Index
include Admin::View
end
end
14 changes: 14 additions & 0 deletions apps/admin/views/settings/new.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Admin::Views::Settings
class New
include Admin::View

def form(settings)
form_for :setting, routes.settings_path, class: 'setting-form' do
label :title
text_field :title, value: settings.title

submit 'Create'
end
end
end
end
3 changes: 1 addition & 2 deletions apps/web/templates/shared/_header.html.slim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h2 Hanami contributors
h2 = title

.contributors_range
ul.nav.nav-pills
Expand All @@ -16,4 +16,3 @@ h2 Hanami contributors
a href='http://hanamirb.org' target='_blank' Main site
li role="presentation"
a href='https://github.com/hanami/contributors' target='_blank' Source

8 changes: 7 additions & 1 deletion apps/web/views/application_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ApplicationLayout
include Web::Layout

def title
'Hanami contributors'
settings.title
end

def all_time_page_class
Expand All @@ -30,6 +30,12 @@ def this_year_page_class
def faq_page_class
nil
end

private

def settings
@settings ||= SettingRepository.new.latest
end
end
end
end
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'bundler/setup'
require 'hanami/setup'
require 'hanami/model'
require_relative '../system/import'
require_relative '../lib/contributors'
require_relative '../apps/admin/application'
require_relative '../apps/api/application'
Expand Down
7 changes: 2 additions & 5 deletions config/initializers/application.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require 'dry/system/container'

# Configure the code that will yield each time Admin::Action is included
# This is useful for sharing common functionality
#
# See: http://www.rubydoc.info/gems/hanami-controller#Configuration
ADMIN_USERNAME = ENV.fetch('ADMIN_USERNAME')
ADMIN_PASSWORD = ENV.fetch('ADMIN_PASSWORD')

class Application < Dry::System::Container
configure
end
# Enable pagination helpers for repositories
CommitRepository.enable_pagination!
1 change: 0 additions & 1 deletion config/initializers/enable_pagination.rb

This file was deleted.

9 changes: 9 additions & 0 deletions db/migrations/20180215045918_create_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hanami::Model.migration do
change do
create_table :settings do
primary_key :id

column :title, String
end
end
end
5 changes: 5 additions & 0 deletions db/migrations/20180219121313_add_created_at_to_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hanami::Model.migration do
change do
add_column :settings, :created_at, DateTime, null: false
end
end
2 changes: 2 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
]

repo = ProjectRepository.new
settings_repo = SettingRepository.new
settings_repo.create(title: 'Hanami')
PROJECTS.each { |name| repo.create(name: name) }
AddNewContributors.new.call
AddNewCommits.new.call
1 change: 0 additions & 1 deletion lib/contributors.rb
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Hanami::Utils.require!("#{__dir__}/contributors")
2 changes: 2 additions & 0 deletions lib/contributors/entities/setting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Setting < Hanami::Entity
end
12 changes: 12 additions & 0 deletions lib/contributors/operations/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'hanami/interactor'

module Operations
class List
include Hanami::Interactor
expose :contributors

def call
@contributors = []
end
end
end
15 changes: 15 additions & 0 deletions lib/contributors/repositories/setting_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class SettingRepository < Hanami::Repository
def history
settings
.order{ created_at.desc }
.map_to(Setting)
.to_a
end

def latest
settings
.order{ created_at.desc }
.limit(1).map_to(Setting)
.one || Setting.new
end
end
20 changes: 6 additions & 14 deletions spec/admin/controllers/projects/index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
RSpec.describe Admin::Controllers::Projects::Index, type: :action do
let(:action) { described_class.new }
let(:params) { Hash[] }
let(:project_repo) { ProjectRepository.new }

it 'is successful' do
response = action.call(params)
expect(response[0]).to eq 200
end
it { expect(action.call(params)).to be_success }

describe 'expose' do
describe '#projects' do
Expand All @@ -16,23 +14,17 @@
end

context 'when db has some projects' do
let(:project_repo) { ProjectRepository.new }

before do
project_repo.create(name: 'contributors')
action.call(params)
end

after do
project_repo.clear
end
after { project_repo.clear }

it 'returns all projects' do
projects = action.projects.to_a
subject { action.projects }

expect(projects).to all(be_a(Project))
expect(projects.count).to eq 1
end
it { expect(subject).to all(be_a(Project)) }
it { expect(subject.count).to eq 1 }
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/admin/controllers/settings/create_spec.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions spec/admin/controllers/settings/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
RSpec.describe Admin::Controllers::Settings::Index, type: :action do
let(:action) { described_class.new }
let(:params) { Hash[] }
let(:settings_repo) { SettingRepository.new }

after { settings_repo.clear }

it { expect(action.call(params)).to be_success }

describe 'expose' do
describe '#setting_history' do
context 'when db empty' do
before { action.call(params) }

it { expect(action.setting_history).to eq [] }
end

context 'when db has settings' do
before do
settings_repo.create(title: 'Hanami')
action.call(params)
end

it { expect(action.setting_history).to be_a Array }
it { expect(action.setting_history).to all(be_a Setting) }
it { expect(action.setting_history.first.title).to eq 'Hanami' }
end
end
end
end
34 changes: 34 additions & 0 deletions spec/admin/features/settings/create_setting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'features_helper'

describe 'Create a setting' do
let(:repo) { SettingRepository.new }
include_context 'authenticated'

after { repo.clear }

context 'No setting before' do
it 'can create a new setting' do
visit '/admin/settings/new'

within 'form#setting-form' do
fill_in 'Title', with: 'Hanami'

click_button 'Create'
end

expect(current_path).to eq('/admin/settings')
expect(page).to have_content('Hanami')
end
end

context 'Previous setting before' do

before { repo.create(title: 'dry-rb') }

it 'preview setting value are displayed' do
visit '/admin/settings/new'

expect(page).to have_field('Title', with: 'dry-rb')
end
end
end
3 changes: 3 additions & 0 deletions spec/contributors/entities/setting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.describe Setting do
# place your tests here
end
7 changes: 7 additions & 0 deletions spec/contributors/operations/list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
RSpec.describe Operations::List do
let(:operation) { described_class.new }
subject { operation.call }

it { expect(subject).to be_successful }
it { expect(subject.contributors).to eq [] }
end
Loading

0 comments on commit 8355656

Please sign in to comment.