Skip to content

Commit

Permalink
add locations controllers and views
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail Akbudak committed Jun 22, 2016
1 parent 90e4810 commit 8d81a6c
Show file tree
Hide file tree
Showing 26 changed files with 385 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/cybele/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ def custom_404
end

def add_seeds
say 'Add seeds'
inject_into_file 'db/seeds.rb', after: "# Mayor.create(name: 'Emanuel', city: cities.first)\n" do <<-RUBY
Admin.create(email: "admin@#{app_name}.com", name: 'Admin', surname: 'Admin', password: '12341234', password_confirmation: '12341234')
RUBY
Expand All @@ -383,8 +382,14 @@ def copy_files
# Model files
remove_file 'app/models/admin.rb', force: true
remove_file 'app/models/user.rb', force: true
remove_file 'app/models/city.rb', force: true
remove_file 'app/models/country.rb', force: true
directory 'app/models', 'app/models'

# Helper files
remove_file 'app/helpers/application_helper.rb', force: true
template 'app/helpers/application_helper.rb.erb', 'app/helpers/application_helper.rb'

# Route file
say 'Restore routes.rb'
remove_file 'config/routes.rb', force: true
Expand Down Expand Up @@ -459,6 +464,11 @@ def git_commands
git :init
end

def create_location_models
generate 'model Country name:string'
generate 'model City name:string country:references'
end

private

def action_mailer_host(rails_env)
Expand All @@ -470,7 +480,7 @@ def action_mailer_host(rails_env)
end

def configure_environment(rails_env, config)
inject_into_file("config/environments/#{rails_env}.rb", "\n\n #{config}", before: "\nend")
inject_into_file("config/environments/#{rails_env}.rb", "\n #{config}", before: "\nend")
end

def generate_devise_strong_parameters(model_name)
Expand Down
5 changes: 5 additions & 0 deletions lib/cybele/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ def setup_namespaces
build :setup_namespaces
end

def setup_models
say 'Setup models'
build :create_location_models
end

def copy_all_files
say 'Copy files'
build :copy_files
Expand Down
54 changes: 54 additions & 0 deletions templates/app/controllers/hq/cities_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Hq::CitiesController < Hq::ApplicationController

before_action :set_city, only: [:show, :edit, :update, :destroy]
add_breadcrumb I18n.t('activerecord.models.cities'), :hq_cities_path

def index
@search = City.includes(:country).order(id: :desc).search(params[:q])
@cities = @search.result(distinct: true).paginate(page: params[:page])
respond_with(@cities)
end

def show
add_breadcrumb @city.name, hq_city_path(@city)
respond_with(@city)
end

def new
add_breadcrumb t('tooltips.new'), new_hq_city_path
@city = City.new
respond_with(@city)
end

def edit
add_breadcrumb @city.name, hq_city_path(@city)
add_breadcrumb t('tooltips.edit'), edit_hq_city_path
end

def create
@city = City.new(city_params)
@city.save
respond_with(:hq, @city)
end

def update
@city.update(city_params)
respond_with(:hq, @city)
end

def destroy
@city.destroy
respond_with(:hq, @city, location: request.referer)
end

private

def city_params
params.require(:city).permit(:name, :country_id)
end

def set_city
@city = City.find(params[:id])
end

end
54 changes: 54 additions & 0 deletions templates/app/controllers/hq/countries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Hq::CountriesController < Hq::ApplicationController

before_action :set_country, only: [:show, :edit, :update, :destroy]
add_breadcrumb I18n.t('activerecord.models.countries'), :hq_countries_path

def index
@search = Country.order(id: :desc).search(params[:q])
@countries = @search.result(distinct: true).paginate(page: params[:page])
respond_with(@countries)
end

def show
add_breadcrumb @country.name, hq_country_path(@country)
respond_with(@country)
end

def new
add_breadcrumb t('tooltips.new'), new_hq_country_path
@country = Country.new
respond_with(@country)
end

def edit
add_breadcrumb @country.name, hq_country_path(@country)
add_breadcrumb t('tooltips.edit'), edit_hq_country_path
end

def create
@country = Country.new(country_params)
@country.save
respond_with(:hq, @country)
end

def update
@country.update(country_params)
respond_with(:hq, @country)
end

def destroy
@country.destroy
respond_with(:hq, @country, location: request.referer)
end

private

def country_params
params.require(:country).permit(:name)
end

def set_country
@country = Country.find(params[:id])
end

end
15 changes: 15 additions & 0 deletions templates/app/helpers/application_helper.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ApplicationHelper

def get_countries
Country.all.map{|c| ["#{c.name}", c.id] }
end

def get_cities
City.all.map{|c| ["#{c.name}", c.id] }
end

def get_active_users
User.active.map{|c| ["#{c.email} - #{c.full_name}", c.id] }
end

end
9 changes: 9 additions & 0 deletions templates/app/models/city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class City < ActiveRecord::Base

# Relations
belongs_to :country

# Validations
validates_presence_of :name, :country_id

end
9 changes: 9 additions & 0 deletions templates/app/models/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Country < ActiveRecord::Base

# Relations
has_many :cities, dependent: :restrict_with_error

# Validations
validates_presence_of :name

end
9 changes: 9 additions & 0 deletions templates/app/views/hq/cities/_blank.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- if params[:q].present?
= render 'list'
- else
.alert.alert-warning
%p
%h3= t('view.there_is_no_data', resource: City.model_name.human.downcase)
= link_to new_hq_city_path, class: 'btn btn-primary' do
= t('action_button.new', resource_name: City.model_name.human)
= t('btn.add')
12 changes: 12 additions & 0 deletions templates/app/views/hq/cities/_city.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%tr
%td= city.id
%td= city.name
%td= city.country.name
%td=l city.created_at
%td.action
= link_to([:hq, city], class: 'btn btn-success', toogle: 'tooltip', title: t('tooltips.zoom')) do
%i.icon-eye-open
= link_to(edit_hq_city_path(city) , class: 'btn btn-info') do
%i.icon-edit
= link_to([:hq, city], class: 'btn btn-danger', method: :delete, data: { confirm: t('tooltips.are_you_sure') }) do
%i.icon-trash
15 changes: 15 additions & 0 deletions templates/app/views/hq/cities/_filters.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.table-header.row
.col-lg-3
- if params[:q].present? && (params[:q][:name_cont].present?)
= link_to hq_cities_path, class: 'btn btn-info' do
= t('view.all')
%span.badge= City.count
.col-lg-9
= search_form_for [:hq, @search], builder: SimpleForm::FormBuilder, html: {class: 'form-inline', data: { turboform: true }} do |f|
.pull-right
= f.input :country_id_eq, label: false, collection: get_countries, include_blank: "#{t('activerecord.attributes.city.country')} #{t('view.select').downcase}", input_html: { class: 'chosen-select form-control'}
.input-group
= f.input_field :name_cont, label: false, class: 'form-control', placeholder: t('view.quick_search')
%span.input-group-btn
= button_tag( class: 'btn btn-primary') do
%i.icon-search
15 changes: 15 additions & 0 deletions templates/app/views/hq/cities/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.panel.panel-default
.panel-heading
%i.icon-edit.icon-large
= yield :form_title
.panel-body
= simple_form_for([:hq, @city]) do |f|
= f.error_notification

.form-inputs
= f.input :name
= f.input :country_id, collection: get_countries, include_blank: t('view.select'), input_html: { class: 'chosen-select' }

.form-actions
= f.button :submit, class: 'btn btn-primary'
= link_to t('cancel'), hq_cities_path, class: 'btn'
13 changes: 13 additions & 0 deletions templates/app/views/hq/cities/_list.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.panel-body.filters
= render 'filters'
%table.table
%thead
%tr
%th= sort_link(@search, :id, t('activerecord.attributes.city.id'))
%th= sort_link(@search, :name, t('activerecord.attributes.city.name'))
%th= sort_link(@search, :country_id, t('activerecord.attributes.city.country_id'))
%th= sort_link(@search, :created_at, t('activerecord.attributes.city.created_at'))
%th.actions
= t('view.actions')
%tbody
= render @cities
3 changes: 3 additions & 0 deletions templates/app/views/hq/cities/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- content_for :form_title do
= t('tt.edit', resource_name: City.model_name.human)
= render 'form'
18 changes: 18 additions & 0 deletions templates/app/views/hq/cities/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- content_for :toolbar do
= link_to new_hq_city_path, class: 'btn btn-default' do
%i.icon-plus
= t('action_button.new', resource_name: City.model_name.human)

.panel.panel-default.grid
.panel-heading
%i.icon-table.icon-large
= t('tt.index', resource_name: City.model_name.human)
.panel-tools
.btn-group
%a.btn{href: hq_cities_path, data: {toggle: 'toolbar-tooltip'}, title: t('view.reload')}
%i.icon-refresh
.badge= @cities.count
=blankable(@cities)
.panel-footer
.pagination.pagination-sm
= will_paginate @cities, renderer: BootstrapPagination::Rails, bootstrap: 3
3 changes: 3 additions & 0 deletions templates/app/views/hq/cities/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- content_for :form_title do
= t('tt.new', resource_name: City.model_name.human)
= render 'form'
13 changes: 13 additions & 0 deletions templates/app/views/hq/cities/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- content_for :toolbar do
= link_to edit_hq_city_path(@city ), class: 'btn btn-default' do
%i.icon-pencil
= t('action_button.edit')
.panel.panel-default
.panel-heading
%i.icon-edit.icon-large
= t('tt.show', resource_name: City.model_name.human)
.panel-body
= show_for @city do |s|
= s.attribute :id
= s.attribute :name
= s.attribute :name, in: :country
9 changes: 9 additions & 0 deletions templates/app/views/hq/countries/_blank.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- if params[:q].present?
= render 'list'
- else
.alert.alert-warning
%p
%h3= t('view.there_is_no_data', resource: Country.model_name.human.downcase)
= link_to new_hq_country_path, class: 'btn btn-primary' do
= t('action_button.new', resource_name: Country.model_name.human)
= t('btn.add')
11 changes: 11 additions & 0 deletions templates/app/views/hq/countries/_country.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%tr
%td= country.id
%td= country.name
%td=l country.created_at
%td.action
= link_to([:hq, country], class: 'btn btn-success', toogle: 'tooltip', title: t('tooltips.zoom')) do
%i.icon-eye-open
= link_to(edit_hq_country_path(country) , class: 'btn btn-info') do
%i.icon-edit
= link_to([:hq, country], class: 'btn btn-danger', method: :delete, data: { confirm: t('tooltips.are_you_sure') }) do
%i.icon-trash
14 changes: 14 additions & 0 deletions templates/app/views/hq/countries/_filters.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.table-header.row
.col-lg-3
- if params[:q].present? && (params[:q][:name_cont].present?)
= link_to hq_countries_path, class: 'btn btn-info' do
= t('view.all')
%span.badge= Country.count
.col-lg-9
= search_form_for [:hq, @search], builder: SimpleForm::FormBuilder, html: {class: 'form-inline', data: { turboform: true }} do |f|
.pull-right
.input-group
= f.input_field :name_cont, label: false, class: 'form-control', placeholder: t('view.quick_search')
%span.input-group-btn
= button_tag( class: 'btn btn-primary') do
%i.icon-search
14 changes: 14 additions & 0 deletions templates/app/views/hq/countries/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.panel.panel-default
.panel-heading
%i.icon-edit.icon-large
= yield :form_title
.panel-body
= simple_form_for([:hq, @country]) do |f|
= f.error_notification

.form-inputs
= f.input :name

.form-actions
= f.button :submit, class: 'btn btn-primary'
= link_to t('cancel'), hq_countries_path, class: 'btn'
12 changes: 12 additions & 0 deletions templates/app/views/hq/countries/_list.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.panel-body.filters
= render 'filters'
%table.table
%thead
%tr
%th= sort_link(@search, :id, t('activerecord.attributes.country.id'))
%th= sort_link(@search, :name, t('activerecord.attributes.country.name'))
%th= sort_link(@search, :created_at, t('activerecord.attributes.country.created_at'))
%th.actions
= t('view.actions')
%tbody
= render @countries
3 changes: 3 additions & 0 deletions templates/app/views/hq/countries/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- content_for :form_title do
= t('tt.edit', resource_name: Country.model_name.human)
= render 'form'
Loading

0 comments on commit 8d81a6c

Please sign in to comment.