From 8d81a6c5320b1263a10ee7d28c5f13c0672b384e Mon Sep 17 00:00:00 2001 From: ismail Akbudak Date: Wed, 22 Jun 2016 14:39:54 +0300 Subject: [PATCH] add locations controllers and views --- lib/cybele/app_builder.rb | 14 ++++- lib/cybele/generators/app_generator.rb | 5 ++ .../app/controllers/hq/cities_controller.rb | 54 +++++++++++++++++++ .../controllers/hq/countries_controller.rb | 54 +++++++++++++++++++ .../app/helpers/application_helper.rb.erb | 15 ++++++ templates/app/models/city.rb | 9 ++++ templates/app/models/country.rb | 9 ++++ .../app/views/hq/cities/_blank.html.haml | 9 ++++ templates/app/views/hq/cities/_city.html.haml | 12 +++++ .../app/views/hq/cities/_filters.html.haml | 15 ++++++ templates/app/views/hq/cities/_form.html.haml | 15 ++++++ templates/app/views/hq/cities/_list.html.haml | 13 +++++ templates/app/views/hq/cities/edit.html.haml | 3 ++ templates/app/views/hq/cities/index.html.haml | 18 +++++++ templates/app/views/hq/cities/new.html.haml | 3 ++ templates/app/views/hq/cities/show.html.haml | 13 +++++ .../app/views/hq/countries/_blank.html.haml | 9 ++++ .../app/views/hq/countries/_country.html.haml | 11 ++++ .../app/views/hq/countries/_filters.html.haml | 14 +++++ .../app/views/hq/countries/_form.html.haml | 14 +++++ .../app/views/hq/countries/_list.html.haml | 12 +++++ .../app/views/hq/countries/edit.html.haml | 3 ++ .../app/views/hq/countries/index.html.haml | 18 +++++++ .../app/views/hq/countries/new.html.haml | 3 ++ .../app/views/hq/countries/show.html.haml | 14 +++++ .../layouts/hq/partials/_dock.html.haml.erb | 28 ++++++++++ 26 files changed, 385 insertions(+), 2 deletions(-) create mode 100755 templates/app/controllers/hq/cities_controller.rb create mode 100755 templates/app/controllers/hq/countries_controller.rb create mode 100644 templates/app/helpers/application_helper.rb.erb create mode 100644 templates/app/models/city.rb create mode 100644 templates/app/models/country.rb create mode 100644 templates/app/views/hq/cities/_blank.html.haml create mode 100644 templates/app/views/hq/cities/_city.html.haml create mode 100644 templates/app/views/hq/cities/_filters.html.haml create mode 100644 templates/app/views/hq/cities/_form.html.haml create mode 100644 templates/app/views/hq/cities/_list.html.haml create mode 100644 templates/app/views/hq/cities/edit.html.haml create mode 100644 templates/app/views/hq/cities/index.html.haml create mode 100644 templates/app/views/hq/cities/new.html.haml create mode 100644 templates/app/views/hq/cities/show.html.haml create mode 100644 templates/app/views/hq/countries/_blank.html.haml create mode 100644 templates/app/views/hq/countries/_country.html.haml create mode 100644 templates/app/views/hq/countries/_filters.html.haml create mode 100644 templates/app/views/hq/countries/_form.html.haml create mode 100644 templates/app/views/hq/countries/_list.html.haml create mode 100644 templates/app/views/hq/countries/edit.html.haml create mode 100644 templates/app/views/hq/countries/index.html.haml create mode 100644 templates/app/views/hq/countries/new.html.haml create mode 100644 templates/app/views/hq/countries/show.html.haml diff --git a/lib/cybele/app_builder.rb b/lib/cybele/app_builder.rb index 09eeaa0..47c4b8f 100644 --- a/lib/cybele/app_builder.rb +++ b/lib/cybele/app_builder.rb @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/lib/cybele/generators/app_generator.rb b/lib/cybele/generators/app_generator.rb index edb588f..120af93 100644 --- a/lib/cybele/generators/app_generator.rb +++ b/lib/cybele/generators/app_generator.rb @@ -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 diff --git a/templates/app/controllers/hq/cities_controller.rb b/templates/app/controllers/hq/cities_controller.rb new file mode 100755 index 0000000..62d04e8 --- /dev/null +++ b/templates/app/controllers/hq/cities_controller.rb @@ -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 diff --git a/templates/app/controllers/hq/countries_controller.rb b/templates/app/controllers/hq/countries_controller.rb new file mode 100755 index 0000000..a187496 --- /dev/null +++ b/templates/app/controllers/hq/countries_controller.rb @@ -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 diff --git a/templates/app/helpers/application_helper.rb.erb b/templates/app/helpers/application_helper.rb.erb new file mode 100644 index 0000000..3f5b279 --- /dev/null +++ b/templates/app/helpers/application_helper.rb.erb @@ -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 diff --git a/templates/app/models/city.rb b/templates/app/models/city.rb new file mode 100644 index 0000000..b103afa --- /dev/null +++ b/templates/app/models/city.rb @@ -0,0 +1,9 @@ +class City < ActiveRecord::Base + + # Relations + belongs_to :country + + # Validations + validates_presence_of :name, :country_id + +end diff --git a/templates/app/models/country.rb b/templates/app/models/country.rb new file mode 100644 index 0000000..f47e165 --- /dev/null +++ b/templates/app/models/country.rb @@ -0,0 +1,9 @@ +class Country < ActiveRecord::Base + + # Relations + has_many :cities, dependent: :restrict_with_error + + # Validations + validates_presence_of :name + +end diff --git a/templates/app/views/hq/cities/_blank.html.haml b/templates/app/views/hq/cities/_blank.html.haml new file mode 100644 index 0000000..ab6784a --- /dev/null +++ b/templates/app/views/hq/cities/_blank.html.haml @@ -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') \ No newline at end of file diff --git a/templates/app/views/hq/cities/_city.html.haml b/templates/app/views/hq/cities/_city.html.haml new file mode 100644 index 0000000..aa849fd --- /dev/null +++ b/templates/app/views/hq/cities/_city.html.haml @@ -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 \ No newline at end of file diff --git a/templates/app/views/hq/cities/_filters.html.haml b/templates/app/views/hq/cities/_filters.html.haml new file mode 100644 index 0000000..efb10b8 --- /dev/null +++ b/templates/app/views/hq/cities/_filters.html.haml @@ -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 \ No newline at end of file diff --git a/templates/app/views/hq/cities/_form.html.haml b/templates/app/views/hq/cities/_form.html.haml new file mode 100644 index 0000000..ed8e69d --- /dev/null +++ b/templates/app/views/hq/cities/_form.html.haml @@ -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' diff --git a/templates/app/views/hq/cities/_list.html.haml b/templates/app/views/hq/cities/_list.html.haml new file mode 100644 index 0000000..b2f780c --- /dev/null +++ b/templates/app/views/hq/cities/_list.html.haml @@ -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 diff --git a/templates/app/views/hq/cities/edit.html.haml b/templates/app/views/hq/cities/edit.html.haml new file mode 100644 index 0000000..9342bf2 --- /dev/null +++ b/templates/app/views/hq/cities/edit.html.haml @@ -0,0 +1,3 @@ +- content_for :form_title do + = t('tt.edit', resource_name: City.model_name.human) += render 'form' \ No newline at end of file diff --git a/templates/app/views/hq/cities/index.html.haml b/templates/app/views/hq/cities/index.html.haml new file mode 100644 index 0000000..70f918a --- /dev/null +++ b/templates/app/views/hq/cities/index.html.haml @@ -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 \ No newline at end of file diff --git a/templates/app/views/hq/cities/new.html.haml b/templates/app/views/hq/cities/new.html.haml new file mode 100644 index 0000000..afc0ad5 --- /dev/null +++ b/templates/app/views/hq/cities/new.html.haml @@ -0,0 +1,3 @@ +- content_for :form_title do + = t('tt.new', resource_name: City.model_name.human) += render 'form' diff --git a/templates/app/views/hq/cities/show.html.haml b/templates/app/views/hq/cities/show.html.haml new file mode 100644 index 0000000..5ca4148 --- /dev/null +++ b/templates/app/views/hq/cities/show.html.haml @@ -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 diff --git a/templates/app/views/hq/countries/_blank.html.haml b/templates/app/views/hq/countries/_blank.html.haml new file mode 100644 index 0000000..6dba0f1 --- /dev/null +++ b/templates/app/views/hq/countries/_blank.html.haml @@ -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') \ No newline at end of file diff --git a/templates/app/views/hq/countries/_country.html.haml b/templates/app/views/hq/countries/_country.html.haml new file mode 100644 index 0000000..4370ea2 --- /dev/null +++ b/templates/app/views/hq/countries/_country.html.haml @@ -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 \ No newline at end of file diff --git a/templates/app/views/hq/countries/_filters.html.haml b/templates/app/views/hq/countries/_filters.html.haml new file mode 100644 index 0000000..2601bb5 --- /dev/null +++ b/templates/app/views/hq/countries/_filters.html.haml @@ -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 \ No newline at end of file diff --git a/templates/app/views/hq/countries/_form.html.haml b/templates/app/views/hq/countries/_form.html.haml new file mode 100644 index 0000000..9a674a9 --- /dev/null +++ b/templates/app/views/hq/countries/_form.html.haml @@ -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' diff --git a/templates/app/views/hq/countries/_list.html.haml b/templates/app/views/hq/countries/_list.html.haml new file mode 100644 index 0000000..dcb0834 --- /dev/null +++ b/templates/app/views/hq/countries/_list.html.haml @@ -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 diff --git a/templates/app/views/hq/countries/edit.html.haml b/templates/app/views/hq/countries/edit.html.haml new file mode 100644 index 0000000..8cec095 --- /dev/null +++ b/templates/app/views/hq/countries/edit.html.haml @@ -0,0 +1,3 @@ +- content_for :form_title do + = t('tt.edit', resource_name: Country.model_name.human) += render 'form' \ No newline at end of file diff --git a/templates/app/views/hq/countries/index.html.haml b/templates/app/views/hq/countries/index.html.haml new file mode 100644 index 0000000..f2e1f03 --- /dev/null +++ b/templates/app/views/hq/countries/index.html.haml @@ -0,0 +1,18 @@ +- content_for :toolbar do + = link_to new_hq_country_path, class: 'btn btn-default' do + %i.icon-plus + = t('action_button.new', resource_name: Country.model_name.human) + +.panel.panel-default.grid + .panel-heading + %i.fa.fa-table.icon-large + = t('tt.index', resource_name: Country.model_name.human) + .panel-tools + .btn-group + %a.btn{href: hq_countries_path, data: {toggle: 'toolbar-tooltip'}, title: t('view.reload')} + %i.fa.fa-refresh + .badge= @countries.total_entries + =blankable(@countries) + .panel-footer + .pagination.pagination-sm + = will_paginate @countries, renderer: BootstrapPagination::Rails, bootstrap: 3 \ No newline at end of file diff --git a/templates/app/views/hq/countries/new.html.haml b/templates/app/views/hq/countries/new.html.haml new file mode 100644 index 0000000..086ad8e --- /dev/null +++ b/templates/app/views/hq/countries/new.html.haml @@ -0,0 +1,3 @@ +- content_for :form_title do + = t('tt.new', resource_name: Country.model_name.human) += render 'form' diff --git a/templates/app/views/hq/countries/show.html.haml b/templates/app/views/hq/countries/show.html.haml new file mode 100644 index 0000000..209f8f4 --- /dev/null +++ b/templates/app/views/hq/countries/show.html.haml @@ -0,0 +1,14 @@ +- content_for :toolbar do + = link_to edit_hq_country_path(@country ), 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: Country.model_name.human) + .panel-body + = show_for @country do |s| + = s.attribute :id + = s.attribute :name + = s.attribute :created_at + = s.attribute :updated_at diff --git a/templates/app/views/layouts/hq/partials/_dock.html.haml.erb b/templates/app/views/layouts/hq/partials/_dock.html.haml.erb index 9c6f378..44536c0 100644 --- a/templates/app/views/layouts/hq/partials/_dock.html.haml.erb +++ b/templates/app/views/layouts/hq/partials/_dock.html.haml.erb @@ -1,8 +1,36 @@ %i#toggle.icon-align-justify.icon-large %ul#dock + %li.launcher %i.icon-dashboard =link_to t('dock.dashboard'), hq_dashboard_index_path + %li.launcher.dropdown.hover + %i.fa.fa-list + %a{href: '#'}= t('dock.system_users') + %ul.dropdown-menu + %li + = link_to hq_admins_path do + %i.fa.fa-users + = t('activerecord.models.admins') + %li + = link_to hq_users_path do + %i.fa.fa-users + = t('activerecord.models.users') + + %br/ + %li.launcher.dropdown.hover + %i.fa.fa-list + %a{href: '#'}= t('dock.system_datas') + %ul.dropdown-menu + %li + =link_to hq_countries_path do + %i.fa.fa-database + = t('activerecord.models.countries') + %li + =link_to hq_cities_path do + %i.fa.fa-database + = t('activerecord.models.cities') + #beaker{data: {toggle: 'tooltip'}, title: 'Made by lab2023'} \ No newline at end of file