Skip to content

Commit

Permalink
add audit controller and shows
Browse files Browse the repository at this point in the history
  • Loading branch information
ismail Akbudak committed Jun 22, 2016
1 parent 4739ee0 commit 1872c56
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/cybele/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
Expand Down
8 changes: 8 additions & 0 deletions templates/app/controllers/application_controller.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
11 changes: 11 additions & 0 deletions templates/app/controllers/hq/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions templates/app/controllers/hq/audits_controller.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions templates/app/controllers/user/user_application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions templates/app/helpers/application_helper.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions templates/app/models/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions templates/app/models/audit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Audit < Audited::Adapters::ActiveRecord::Audit

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

# Helpers
audited

# Relations
belongs_to :country

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

# Helpers
audited

# Relations
has_many :cities, dependent: :restrict_with_error

Expand Down
3 changes: 3 additions & 0 deletions templates/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions templates/app/views/hq/audits/_filters.html.haml
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions templates/app/views/hq/audits/_list.html.haml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions templates/app/views/hq/audits/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.panel.panel-default.grid
= render 'list'
.panel-footer
.pagination.pagination-sm
= will_paginate @audits, renderer: BootstrapPagination::Rails, bootstrap: 3
5 changes: 5 additions & 0 deletions templates/app/views/hq/audits/show.html.haml
Original file line number Diff line number Diff line change
@@ -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]
7 changes: 6 additions & 1 deletion templates/config/locales/view.tr.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
tr:
tr:
actions:
update: Güncelleme
create: Oluşturma
destroy: Silme
delete: Silme
btn:
back: Geri
update: Güncelle
Expand Down
4 changes: 3 additions & 1 deletion templates/cybele_Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'

0 comments on commit 1872c56

Please sign in to comment.