From 1872c567849eb1e99c10e4f19250705a2a119b78 Mon Sep 17 00:00:00 2001 From: ismail Akbudak Date: Wed, 22 Jun 2016 16:28:10 +0300 Subject: [PATCH] add audit controller and shows --- lib/cybele/app_builder.rb | 15 ++++++++ lib/cybele/generators/app_generator.rb | 5 +++ .../controllers/application_controller.rb.erb | 8 ++++ .../controllers/hq/application_controller.rb | 11 ++++++ .../app/controllers/hq/audits_controller.rb | 16 ++++++++ .../user/user_application_controller.rb | 11 ++++++ .../app/helpers/application_helper.rb.erb | 12 ++++++ templates/app/models/admin.rb | 3 ++ templates/app/models/audit.rb | 3 ++ templates/app/models/city.rb | 3 ++ templates/app/models/country.rb | 3 ++ templates/app/models/user.rb | 3 ++ .../app/views/hq/audits/_filters.html.haml | 32 ++++++++++++++++ templates/app/views/hq/audits/_list.html.haml | 37 +++++++++++++++++++ templates/app/views/hq/audits/index.html.haml | 5 +++ templates/app/views/hq/audits/show.html.haml | 5 +++ templates/config/locales/view.tr.yml | 7 +++- templates/cybele_Gemfile | 4 +- 18 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 templates/app/controllers/hq/audits_controller.rb create mode 100644 templates/app/models/audit.rb create mode 100644 templates/app/views/hq/audits/_filters.html.haml create mode 100644 templates/app/views/hq/audits/_list.html.haml create mode 100644 templates/app/views/hq/audits/index.html.haml create mode 100644 templates/app/views/hq/audits/show.html.haml diff --git a/lib/cybele/app_builder.rb b/lib/cybele/app_builder.rb index 47c4b8f..874b6d4 100644 --- a/lib/cybele/app_builder.rb +++ b/lib/cybele/app_builder.rb @@ -467,6 +467,21 @@ def git_commands def create_location_models generate 'model Country name:string' generate 'model City name:string country:references' + generate 'audited:install' + end + + def create_jobs_helper_lib + create_file "lib/jobs_helper.rb", <<-CODE +# Get system admin +def system_admin + admin = Admin.where(email: "system@#{app_name}.com").first + if admin.nil? + password = Devise.friendly_token.first(12) + admin = Admin.create(name: 'System', surname: 'System', email: "system@#{app_name}.com", password: password, password_confirmation: password) + end + admin +end + CODE end private diff --git a/lib/cybele/generators/app_generator.rb b/lib/cybele/generators/app_generator.rb index 120af93..787a2ef 100644 --- a/lib/cybele/generators/app_generator.rb +++ b/lib/cybele/generators/app_generator.rb @@ -255,6 +255,11 @@ def copy_all_files build :copy_files end + def setup_helpers + say 'Create helpers' + build :create_jobs_helper_lib + end + def setup_git say 'Initialize git' build :git_commands diff --git a/templates/app/controllers/application_controller.rb.erb b/templates/app/controllers/application_controller.rb.erb index 136ad95..7754a7c 100644 --- a/templates/app/controllers/application_controller.rb.erb +++ b/templates/app/controllers/application_controller.rb.erb @@ -12,6 +12,7 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. + before_filter :set_audit_user protect_from_forgery with: :exception def server_error(exception) @@ -37,4 +38,11 @@ class ApplicationController < ActionController::Base end end + private + + def set_audit_user + # Set audit current user + Audited.current_user_method = :current_user + end + end \ No newline at end of file diff --git a/templates/app/controllers/hq/application_controller.rb b/templates/app/controllers/hq/application_controller.rb index cf81c60..dbf323d 100644 --- a/templates/app/controllers/hq/application_controller.rb +++ b/templates/app/controllers/hq/application_controller.rb @@ -1,7 +1,18 @@ require 'application_responder' class Hq::ApplicationController < ActionController::Base + + before_filter :set_audit_user before_action :authenticate_admin! + self.responder = ApplicationResponder respond_to :html, :json + + private + + def set_audit_user + # Set audit current user + Audited.current_user_method = :current_admin + end + end \ No newline at end of file diff --git a/templates/app/controllers/hq/audits_controller.rb b/templates/app/controllers/hq/audits_controller.rb new file mode 100644 index 0000000..125196c --- /dev/null +++ b/templates/app/controllers/hq/audits_controller.rb @@ -0,0 +1,16 @@ +class Hq::AuditsController < Hq::ApplicationController + + add_breadcrumb I18n.t('activerecord.models.audits'), :hq_audits_path + + def index + @search = Audit.includes(:user).reorder('id DESC').search(params[:q]) + @audits = @search.result(distinct: true).paginate(page: params[:page]) + @auditable_types = Audited::Adapters::ActiveRecord::Audit.select('auditable_type').group('auditable_type').reorder('') + end + + def show + @audit = Audit.find(params[:id]) + add_breadcrumb @audit.id, hq_audit_path(id: @audit) + end + +end diff --git a/templates/app/controllers/user/user_application_controller.rb b/templates/app/controllers/user/user_application_controller.rb index 6e02da8..d4fefe5 100644 --- a/templates/app/controllers/user/user_application_controller.rb +++ b/templates/app/controllers/user/user_application_controller.rb @@ -1,9 +1,20 @@ require 'application_responder' class User::UserApplicationController < ActionController::Base + # layout 'user/application' layout 'application' + before_filter :set_audit_user before_action :authenticate_user! + self.responder = ApplicationResponder respond_to :html, :json + + private + + def set_audit_user + # Set audit current user + Audited.current_user_method = :current_user + end + end \ No newline at end of file diff --git a/templates/app/helpers/application_helper.rb.erb b/templates/app/helpers/application_helper.rb.erb index 3f5b279..f0f479c 100644 --- a/templates/app/helpers/application_helper.rb.erb +++ b/templates/app/helpers/application_helper.rb.erb @@ -12,4 +12,16 @@ module ApplicationHelper User.active.map{|c| ["#{c.email} - #{c.full_name}", c.id] } end + def get_users + User.map{|c| ["#{c.email} - #{c.full_name}", c.id] } + end + + def get_admins + Admin.map{|c| ["#{c.email} - #{c.full_name}", c.id] } + end + + def get_audit_users + [[t('activerecord.models.admin'), 'Admin'], [t('activerecord.models.user'), 'User']] + end + end diff --git a/templates/app/models/admin.rb b/templates/app/models/admin.rb index 5809bad..bf79ee6 100644 --- a/templates/app/models/admin.rb +++ b/templates/app/models/admin.rb @@ -11,6 +11,9 @@ class Admin < ActiveRecord::Base :trackable, :validatable + # Helpers + audited except: [:password] + # Validations validates_presence_of :name, :email, :surname validates :email, uniqueness: true diff --git a/templates/app/models/audit.rb b/templates/app/models/audit.rb new file mode 100644 index 0000000..ea4f541 --- /dev/null +++ b/templates/app/models/audit.rb @@ -0,0 +1,3 @@ +class Audit < Audited::Adapters::ActiveRecord::Audit + +end \ No newline at end of file diff --git a/templates/app/models/city.rb b/templates/app/models/city.rb index b103afa..8b1b529 100644 --- a/templates/app/models/city.rb +++ b/templates/app/models/city.rb @@ -1,5 +1,8 @@ class City < ActiveRecord::Base + # Helpers + audited + # Relations belongs_to :country diff --git a/templates/app/models/country.rb b/templates/app/models/country.rb index f47e165..737399b 100644 --- a/templates/app/models/country.rb +++ b/templates/app/models/country.rb @@ -1,5 +1,8 @@ class Country < ActiveRecord::Base + # Helpers + audited + # Relations has_many :cities, dependent: :restrict_with_error diff --git a/templates/app/models/user.rb b/templates/app/models/user.rb index 6dc1a90..2a90ebe 100644 --- a/templates/app/models/user.rb +++ b/templates/app/models/user.rb @@ -15,6 +15,9 @@ class User < ActiveRecord::Base :trackable, :validatable + # Helpers + audited except: [:password] + # Validations validates_presence_of :name, :email, :surname validates :email, uniqueness: true diff --git a/templates/app/views/hq/audits/_filters.html.haml b/templates/app/views/hq/audits/_filters.html.haml new file mode 100644 index 0000000..953c1c0 --- /dev/null +++ b/templates/app/views/hq/audits/_filters.html.haml @@ -0,0 +1,32 @@ +.table-header.row + .col-lg-2 + - if params[:q].present? + = link_to hq_audits_path, class: 'btn btn-info' do + = t('view.all') + %span.badge= Audit.count + .col-lg-10 + .pull-right + = search_form_for @search, builder: SimpleForm::FormBuilder, html: {class: 'form-inline'}, url: hq_audits_path do |f| + .form-group + = f.label :user_type, label: t('activerecord.attributes.audits.user_type') + = f.input_field :user_type_eq, label: false, class: 'form-control', placeholder: t('activerecord.attributes.audits.user'), collection: get_audit_users, include_blank: t('view.select') + + - if params[:q].present? and params[:q][:user_type_eq].present? + - type = params[:q][:user_type_eq] + - if type == 'Admin' + = f.label :user, label: t('activerecord.attributes.audits.user') + = f.input :user_id_eq, label: false, class: 'form-control chosen-select', placeholder: t('activerecord.attributes.audits.user'), collection: get_admins, include_blank: t('view.select') + - elsif type == 'User' + = f.label :user, label: t('activerecord.attributes.audits.user') + = f.input_field :user_id_eq, label: false, class: 'form-control chosen-select', placeholder: t('activerecord.attributes.audits.user'), collection: get_users, include_blank: t('view.select') + + = f.label :auditable_type, label: t('activerecord.attributes.audits.auditable_type') + = f.input_field :auditable_type_eq, label: false, class: 'form-control chosen-select', placeholder: t('activerecord.attributes.audits.auditable_type'), collection: @auditable_types.map{|a| [t("activerecord.models.#{a.auditable_type.try(:underscore)}"), a.auditable_type]}, include_blank: t('view.select') + + = f.label :user, label: t('activerecord.attributes.audits.auditable_id') + = f.input_field :auditable_id_eq, label: false, class: 'form-control', placeholder: t('activerecord.attributes.audits.auditable_id') + + = f.label :user, label: t('activerecord.attributes.audits.action') + = f.input_field :action_eq, label: false, class: 'form-control', placeholder: t('activerecord.attributes.audits.action'), collection: %w(create update destroy).map{|a| [t("actions.#{a}"), a]}, include_blank: t('view.select') + = button_tag( class: 'btn btn-success btn-group') do + %i.fa.fa-search \ No newline at end of file diff --git a/templates/app/views/hq/audits/_list.html.haml b/templates/app/views/hq/audits/_list.html.haml new file mode 100644 index 0000000..bc8024e --- /dev/null +++ b/templates/app/views/hq/audits/_list.html.haml @@ -0,0 +1,37 @@ +.panel-heading + %i.fa.fa-list + = t('activerecord.models.audits') + .panel-tools + .btn-group + %a.btn{href: hq_audits_path, data: {toggle: 'toolbar-tooltip'}, title: t('view.reload')} + %i.icon-refresh + .badge= @audits.total_entries +.panel-body.filters + = render 'filters' +.table-responsive + %table.table + %thead + %tr + %th= sort_link(@search, :id, t('activerecord.attributes.audits.id')) + %th= sort_link(@search, :user_type, t('activerecord.attributes.audits.user_type')) + %th= sort_link(@search, :user_id, t('activerecord.attributes.audits.user')) + %th= sort_link(@search, :auditable_type, t('activerecord.attributes.audits.auditable_type')) + %th= sort_link(@search, :auditable_id, t('activerecord.attributes.audits.auditable_id')) + %th= sort_link(@search, :auditable_id, t('activerecord.attributes.audits.created_at')) + %th= sort_link(@search, :action, t('activerecord.attributes.audits.action')) + %th.actions + = t('view.actions') + %tbody + - @audits.each do |audit| + %tr + %td= audit.id + %td + = t("activerecord.models.#{audit.try(:user_type).try(:downcase)}") if audit.try(:user_type).present? + %td= "#{audit.try(:user).try(:full_name)} - #{audit.try(:user).try(:email)}" + %td= t("activerecord.models.#{audit.try(:auditable_type).try(:underscore)}") if audit.try(:auditable_type).present? + %td= audit.auditable_id + %td= l audit.created_at + %td= t("actions.#{audit.action}") + %td.actions + = link_to(hq_audit_path(id: audit), class: 'btn btn-success btn-xs', 'data-toggle' => 'tooltip', title: t('tooltips.zoom')) do + %i.fa.fa-eye \ No newline at end of file diff --git a/templates/app/views/hq/audits/index.html.haml b/templates/app/views/hq/audits/index.html.haml new file mode 100644 index 0000000..eac51e8 --- /dev/null +++ b/templates/app/views/hq/audits/index.html.haml @@ -0,0 +1,5 @@ +.panel.panel-default.grid + = render 'list' + .panel-footer + .pagination.pagination-sm + = will_paginate @audits, renderer: BootstrapPagination::Rails, bootstrap: 3 diff --git a/templates/app/views/hq/audits/show.html.haml b/templates/app/views/hq/audits/show.html.haml new file mode 100644 index 0000000..b1d45d6 --- /dev/null +++ b/templates/app/views/hq/audits/show.html.haml @@ -0,0 +1,5 @@ +- @audit.audited_changes.keys.each do |key| + %p + %strong + = t("activerecord.attributes.#{@audit.try(:auditable_type).try(:underscore)}.#{key}") if @audit.try(:auditable_type).present? + = @audit.audited_changes[key] diff --git a/templates/config/locales/view.tr.yml b/templates/config/locales/view.tr.yml index 223665c..9e009e0 100644 --- a/templates/config/locales/view.tr.yml +++ b/templates/config/locales/view.tr.yml @@ -1,5 +1,10 @@ --- -tr: +tr: + actions: + update: Güncelleme + create: Oluşturma + destroy: Silme + delete: Silme btn: back: Geri update: Güncelle diff --git a/templates/cybele_Gemfile b/templates/cybele_Gemfile index 060f4df..7cefeb2 100644 --- a/templates/cybele_Gemfile +++ b/templates/cybele_Gemfile @@ -46,7 +46,6 @@ gem 'hierapolis-rails', '~> 1.1.3' gem 'sass-rails', '~> 5.0.3' gem 'compass-rails', '~> 2.0.5' gem 'bootstrap-sass', '~> 3.3.4' -gem 'audited-activerecord', '~> 4.0' gem 'enum_help', '~> 0.0.14' gem 'chosen-rails', '1.4.1' gem 'bootstrap-chosen-rails', '0.0.4' @@ -100,5 +99,8 @@ gem 'phony_rails', '~> 0.12.11' # Excell export gem 'to_xls', '~> 1.5', '>= 1.5.3' +# Keep user activities +gem 'audited-activerecord', '~> 4.2' + # GSM service # gem 'bulutfon_sdk', '~> 1.1', '>= 1.1.3' \ No newline at end of file