From c98c53de760d1326cfb0311b78d067cf0c5e98ac Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Fri, 14 Dec 2012 00:03:11 -0500 Subject: [PATCH] Got logs working for users and bikes Not my best work, but it works, need to add transactions, need to add edit/show actions --- .../acts_as_loggable/logs_controller.rb | 49 +++++++++++++ app/controllers/bike_logs_controller.rb | 9 +++ app/controllers/user_logs_controller.rb | 9 +++ app/controllers/users_controller.rb | 34 +++++++++ app/models/acts_as_loggable/bike_action.rb | 1 - app/models/bike.rb | 1 + app/models/user.rb | 1 + .../acts_as_loggable/logs/_form.html.haml | 47 ++++++++++++ .../acts_as_loggable/logs/index.html.haml | 36 +++++++++ app/views/acts_as_loggable/logs/new.html.haml | 4 + app/views/users/_form.html.haml | 68 +++++++++++++++++ app/views/users/edit.html.haml | 4 + app/views/users/index.html.haml | 27 +++++++ app/views/users/new.html.haml | 4 + app/views/users/show.html.haml | 73 +++++++++++++++++++ config/routes.rb | 65 +++-------------- 16 files changed, 378 insertions(+), 54 deletions(-) create mode 100644 app/controllers/acts_as_loggable/logs_controller.rb create mode 100644 app/controllers/bike_logs_controller.rb create mode 100644 app/controllers/user_logs_controller.rb create mode 100644 app/controllers/users_controller.rb create mode 100644 app/views/acts_as_loggable/logs/_form.html.haml create mode 100644 app/views/acts_as_loggable/logs/index.html.haml create mode 100644 app/views/acts_as_loggable/logs/new.html.haml create mode 100644 app/views/users/_form.html.haml create mode 100644 app/views/users/edit.html.haml create mode 100644 app/views/users/index.html.haml create mode 100644 app/views/users/new.html.haml create mode 100644 app/views/users/show.html.haml diff --git a/app/controllers/acts_as_loggable/logs_controller.rb b/app/controllers/acts_as_loggable/logs_controller.rb new file mode 100644 index 0000000..aa218a2 --- /dev/null +++ b/app/controllers/acts_as_loggable/logs_controller.rb @@ -0,0 +1,49 @@ +class ActsAsLoggable::LogsController < AuthenticatedController + before_filter :set_loggable_id + + def index + if params[:loggable_id] + @logs = ActsAsLoggable::Log.where( :loggable_type => @loggable_type, :loggable_id => @loggable_id).order('id').paginate(:page => params[:page]) + set_loggable_path + else + @logs = ActsAsLoggable::Log.order('id').paginate(:page => params[:page]) + end + end + + def show + end + + def new + @log = ActsAsLoggable::Log.new(:loggable_type => @loggable_type, :loggable_id => @loggable_id) + set_loggable_path + end + + def create + params[:acts_as_loggable_log][:logger_id] = current_user.id.to_s + params[:acts_as_loggable_log][:logger_type] = current_user.class.to_s + log = ActsAsLoggable::Log.new(params[:acts_as_loggable_log]) + if log.save + set_loggable_path + redirect_to @loggable_path + else + puts log.errors.inspect + render :new + end + end + + def update + end + + def destroy + end + + private + + def set_loggable_id + @loggable_id = params[:loggable_id] + end + + def set_loggable_path + @loggable_path = "/#{@loggable_type.pluralize.downcase}/#{@loggable_id}/logs" + end +end diff --git a/app/controllers/bike_logs_controller.rb b/app/controllers/bike_logs_controller.rb new file mode 100644 index 0000000..25ec7fa --- /dev/null +++ b/app/controllers/bike_logs_controller.rb @@ -0,0 +1,9 @@ +class BikeLogsController < ActsAsLoggable::LogsController + before_filter :set_loggable_type + + private + + def set_loggable_type + @loggable_type = 'Bike' + end +end diff --git a/app/controllers/user_logs_controller.rb b/app/controllers/user_logs_controller.rb new file mode 100644 index 0000000..c3eb142 --- /dev/null +++ b/app/controllers/user_logs_controller.rb @@ -0,0 +1,9 @@ +class UserLogsController < ActsAsLoggable::LogsController + before_filter :set_loggable_type + + private + + def set_loggable_type + @loggable_type = 'User' + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..ed62891 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,34 @@ +class UsersController < AuthenticatedController + expose(:user) + expose(:users) { User.order('id').paginate(:page => params[:page]) } + + def index + end + + def show + end + + def new + end + + def create + if user.save + redirect_to user + else + render :new + end + end + + def update + if user.save + redirect_to user + else + render :edit + end + end + + def destroy + user.destroy + redirect_to bikes_url + end +end diff --git a/app/models/acts_as_loggable/bike_action.rb b/app/models/acts_as_loggable/bike_action.rb index 696abaa..cdc2d07 100644 --- a/app/models/acts_as_loggable/bike_action.rb +++ b/app/models/acts_as_loggable/bike_action.rb @@ -1,5 +1,4 @@ class ActsAsLoggable::BikeAction < ActiveRecord::Base - #set_fixture_class :bike_actions => ActsAsLoggable::BikeActions attr_accessible :action belongs_to :bike diff --git a/app/models/bike.rb b/app/models/bike.rb index e716bf9..b57b59f 100644 --- a/app/models/bike.rb +++ b/app/models/bike.rb @@ -1,4 +1,5 @@ class Bike < ActiveRecord::Base + acts_as_loggable attr_accessible :serial_number, :bike_brand_id, :bike_model_id, :color, :bike_style_id, :seat_tube_height, :top_tube_length, :wheel_size, :value, :bike_condition_id, :bike_status_id diff --git a/app/models/user.rb b/app/models/user.rb index 64f217a..609861f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ActiveRecord::Base + acts_as_loggable # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, diff --git a/app/views/acts_as_loggable/logs/_form.html.haml b/app/views/acts_as_loggable/logs/_form.html.haml new file mode 100644 index 0000000..efe0dc3 --- /dev/null +++ b/app/views/acts_as_loggable/logs/_form.html.haml @@ -0,0 +1,47 @@ += form_for @log, :html => { :class => 'form-horizontal' }, :url => @loggable_path do |f| + - if @log.loggable_type or @log.loggable_id + = f.hidden_field :loggable_id, :class => 'number_field' + = f.hidden_field :loggable_type, :class => 'text_field' + = f.hidden_field :logger_id, :class => 'number_field' + = f.hidden_field :logger_type, :class => 'text_field' + = f.hidden_field :context, :class => 'text_field' + - else + .control-group + = f.label :loggable_id, :class => 'control-label' + .controls + = f.number_field :loggable_id, :class => 'number_field' + .control-group + = f.label :loggable_type, :class => 'control-label' + .controls + = f.text_field :loggable_type, :class => 'text_field' + .control-group + = f.label :logger_id, :class => 'control-label' + .controls + = f.number_field :logger_id, :class => 'number_field' + .control-group + = f.label :logger_type, :class => 'control-label' + .controls + = f.text_field :logger_type, :class => 'text_field' + .control-group + = f.label :context, :class => 'control-label' + .controls + = f.text_field :context, :class => 'text_field' + .control-group + = f.label :start_date, :class => 'control-label' + .controls + = f.datetime_select :start_date, :class => 'datetime_select' + .control-group + = f.label :end_date, :class => 'control-label' + .controls + = f.datetime_select :end_date, :class => 'datetime_select' + .control-group + = f.label :description, :class => 'control-label' + .controls + = f.text_area :description, :class => 'text_area' + .control-group + = f.label :action_id, :class => 'control-label' + .controls + = f.number_field :action_id, :class => 'number_field' + .form-actions + = f.submit nil, :class => 'btn btn-primary' + = link_to t('.cancel', :default => t("helpers.links.cancel")), @loggable_path, :class => 'btn' diff --git a/app/views/acts_as_loggable/logs/index.html.haml b/app/views/acts_as_loggable/logs/index.html.haml new file mode 100644 index 0000000..581ac39 --- /dev/null +++ b/app/views/acts_as_loggable/logs/index.html.haml @@ -0,0 +1,36 @@ +- model_class = ActsAsLoggable::Log.new.class +.page-header + %h1=t '.title', :default => model_class.model_name.human.pluralize +%table.table.table-striped + %thead + %tr + %th= model_class.human_attribute_name(:id) + %th= model_class.human_attribute_name(:loggable_id) + %th= model_class.human_attribute_name(:loggable_type) + %th= model_class.human_attribute_name(:logger_id) + %th= model_class.human_attribute_name(:logger_type) + %th= model_class.human_attribute_name(:context) + %th= model_class.human_attribute_name(:start_date) + %th= model_class.human_attribute_name(:end_date) + %th= model_class.human_attribute_name(:description) + %th= model_class.human_attribute_name(:action_id) + %th= model_class.human_attribute_name(:created_at) + %th=t '.actions', :default => t("helpers.actions") + %tbody + - @logs.each do |log| + %tr + %td= link_to log.id, acts_as_loggable_log_path(log) + %td= log.loggable_id + %td= log.loggable_type + %td= log.logger_id + %td= log.logger_type + %td= log.context + %td= log.start_date + %td= log.end_date + %td= log.description + %td= log.action + %td=l log.created_at + %td + = link_to t('.edit', :default => t("helpers.links.edit")), edit_acts_as_loggable_log_path(log), :class => 'btn btn-mini' + = link_to t('.destroy', :default => t("helpers.links.destroy")), acts_as_loggable_log_path(log), :method => :delete, :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')), :class => 'btn btn-mini btn-danger' += link_to t('.new', :default => t("helpers.links.new")), "#{@loggable_path}/new", :class => 'btn btn-primary' diff --git a/app/views/acts_as_loggable/logs/new.html.haml b/app/views/acts_as_loggable/logs/new.html.haml new file mode 100644 index 0000000..4b0a323 --- /dev/null +++ b/app/views/acts_as_loggable/logs/new.html.haml @@ -0,0 +1,4 @@ +- model_class = @log.class +.page-header + %h1=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human, :default => "New #{model_class.model_name.human}") += render :partial => "form" diff --git a/app/views/users/_form.html.haml b/app/views/users/_form.html.haml new file mode 100644 index 0000000..df83a93 --- /dev/null +++ b/app/views/users/_form.html.haml @@ -0,0 +1,68 @@ += form_for @user, :html => { :class => 'form-horizontal' } do |f| + .control-group + = f.label :email, :class => 'control-label' + .controls + = f.text_field :email, :class => 'text_field' + .control-group + = f.label :encrypted_password, :class => 'control-label' + .controls + = f.text_field :encrypted_password, :class => 'text_field' + .control-group + = f.label :reset_password_token, :class => 'control-label' + .controls + = f.text_field :reset_password_token, :class => 'text_field' + .control-group + = f.label :reset_password_sent_at, :class => 'control-label' + .controls + = f.datetime_select :reset_password_sent_at, :class => 'datetime_select' + .control-group + = f.label :remember_created_at, :class => 'control-label' + .controls + = f.datetime_select :remember_created_at, :class => 'datetime_select' + .control-group + = f.label :sign_in_count, :class => 'control-label' + .controls + = f.number_field :sign_in_count, :class => 'number_field' + .control-group + = f.label :current_sign_in_at, :class => 'control-label' + .controls + = f.datetime_select :current_sign_in_at, :class => 'datetime_select' + .control-group + = f.label :last_sign_in_at, :class => 'control-label' + .controls + = f.datetime_select :last_sign_in_at, :class => 'datetime_select' + .control-group + = f.label :current_sign_in_ip, :class => 'control-label' + .controls + = f.text_field :current_sign_in_ip, :class => 'text_field' + .control-group + = f.label :last_sign_in_ip, :class => 'control-label' + .controls + = f.text_field :last_sign_in_ip, :class => 'text_field' + .control-group + = f.label :failed_attempts, :class => 'control-label' + .controls + = f.number_field :failed_attempts, :class => 'number_field' + .control-group + = f.label :unlock_token, :class => 'control-label' + .controls + = f.text_field :unlock_token, :class => 'text_field' + .control-group + = f.label :locked_at, :class => 'control-label' + .controls + = f.datetime_select :locked_at, :class => 'datetime_select' + .control-group + = f.label :first_name, :class => 'control-label' + .controls + = f.text_field :first_name, :class => 'text_field' + .control-group + = f.label :last_name, :class => 'control-label' + .controls + = f.text_field :last_name, :class => 'text_field' + .control-group + = f.label :nickname, :class => 'control-label' + .controls + = f.text_field :nickname, :class => 'text_field' + .form-actions + = f.submit nil, :class => 'btn btn-primary' + = link_to t('.cancel', :default => t("helpers.links.cancel")), users_path, :class => 'btn' diff --git a/app/views/users/edit.html.haml b/app/views/users/edit.html.haml new file mode 100644 index 0000000..6bd83b2 --- /dev/null +++ b/app/views/users/edit.html.haml @@ -0,0 +1,4 @@ +- model_class = user.class +.page-header + %h1=t '.title', :default => t('helpers.titles.edit', :model => model_class.model_name.human, :default => "Edit #{model_class.model_name.human}") += render :partial => "form" diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml new file mode 100644 index 0000000..20ee95d --- /dev/null +++ b/app/views/users/index.html.haml @@ -0,0 +1,27 @@ +- model_class = User.new.class +.page-header + %h1=t '.title', :default => model_class.model_name.human.pluralize +%table.table.table-striped + %thead + %tr + %th= model_class.human_attribute_name(:id) + %th= model_class.human_attribute_name(:email) + %th= model_class.human_attribute_name(:first_name) + %th= model_class.human_attribute_name(:last_name) + %th= model_class.human_attribute_name(:nickname) + %th= model_class.human_attribute_name(:created_at) + %th=t '.actions', :default => t("helpers.actions") + %tbody + - users.each do |user| + %tr + %td= link_to user.id, user_path(user) + %td= link_to user.email, user_path(user) + %td= user.first_name + %td= user.last_name + %td= user.nickname + %td=l user.created_at + %td + = link_to t('.edit', :default => t("helpers.links.edit")), edit_user_path(user), :class => 'btn btn-mini' + = link_to t('.destroy', :default => t("helpers.links.destroy")), user_path(user), :method => :delete, :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')), :class => 'btn btn-mini btn-danger' + += link_to t('.new', :default => t("helpers.links.new")), new_user_path, :class => 'btn btn-primary' diff --git a/app/views/users/new.html.haml b/app/views/users/new.html.haml new file mode 100644 index 0000000..5252fa4 --- /dev/null +++ b/app/views/users/new.html.haml @@ -0,0 +1,4 @@ +- model_class = user.class +.page-header + %h1=t '.title', :default => t('helpers.titles.new', :model => model_class.model_name.human, :default => "New #{model_class.model_name.human}") += render :partial => "form" diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml new file mode 100644 index 0000000..4387703 --- /dev/null +++ b/app/views/users/show.html.haml @@ -0,0 +1,73 @@ +- model_class = user.class +.page-header + %h1=t '.title', :default => model_class.model_name.human + +%p + %strong= model_class.human_attribute_name(:email) + ':' + %br + = user.email +%p + %strong= model_class.human_attribute_name(:encrypted_password) + ':' + %br + = user.encrypted_password +%p + %strong= model_class.human_attribute_name(:reset_password_token) + ':' + %br + = user.reset_password_token +%p + %strong= model_class.human_attribute_name(:reset_password_sent_at) + ':' + %br + = user.reset_password_sent_at +%p + %strong= model_class.human_attribute_name(:remember_created_at) + ':' + %br + = user.remember_created_at +%p + %strong= model_class.human_attribute_name(:sign_in_count) + ':' + %br + = user.sign_in_count +%p + %strong= model_class.human_attribute_name(:current_sign_in_at) + ':' + %br + = user.current_sign_in_at +%p + %strong= model_class.human_attribute_name(:last_sign_in_at) + ':' + %br + = user.last_sign_in_at +%p + %strong= model_class.human_attribute_name(:current_sign_in_ip) + ':' + %br + = user.current_sign_in_ip +%p + %strong= model_class.human_attribute_name(:last_sign_in_ip) + ':' + %br + = user.last_sign_in_ip +%p + %strong= model_class.human_attribute_name(:failed_attempts) + ':' + %br + = user.failed_attempts +%p + %strong= model_class.human_attribute_name(:unlock_token) + ':' + %br + = user.unlock_token +%p + %strong= model_class.human_attribute_name(:locked_at) + ':' + %br + = user.locked_at +%p + %strong= model_class.human_attribute_name(:first_name) + ':' + %br + = user.first_name +%p + %strong= model_class.human_attribute_name(:last_name) + ':' + %br + = user.last_name +%p + %strong= model_class.human_attribute_name(:nickname) + ':' + %br + = user.nickname + +.form-actions + = link_to t('.back', :default => t("helpers.links.back")), users_path, :class => 'btn' + = link_to t('.edit', :default => t("helpers.links.edit")), edit_user_path(user), :class => 'btn' + = link_to t('.destroy', :default => t("helpers.links.destroy")), user_path(user), :method => "delete", :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')), :class => 'btn btn-danger' diff --git a/config/routes.rb b/config/routes.rb index 5a4a90a..e168ead 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,6 @@ Velocipede::Application.routes.draw do devise_for :users - # The priority is based upon order of creation: # first created -> highest priority. @@ -13,66 +12,26 @@ Velocipede::Application.routes.draw do resources :bike_styles resources :bike_conditions resources :bikes - + namespace :acts_as_loggable do + resources :logs resources :bike_actions resources :user_actions resources :transaction_actions end - #resources :clues - #resources :maps - - # Sample of regular route: - # match 'products/:id' => 'catalog#view' - # Keep in mind you can assign values other than :controller and :action - - # Sample of named route: - # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase - # This route can be invoked with purchase_url(:id => product.id) - - # Sample resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Sample resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Sample resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - # Sample resource route with more complex sub-resources - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', :on => :collection - # end - # end - # Sample resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + #user logs + get 'users/:loggable_id/logs' => 'user_logs#index', :as => 'user_logs' + get 'users/:loggable_id/logs/new' => 'user_logs#new', :as => 'new_user_log' + post 'users/:loggable_id/logs' => 'user_logs#create' - # You can have the root of your site routed with "root" - # just remember to delete public/index.html. - root :to => 'site#index' + #bike logs + get 'bikes/:loggable_id/logs' => 'bike_logs#index', :as => 'bike_logs' + get 'bikes/:loggable_id/logs/new' => 'bike_logs#new', :as => 'new_bike_log' + post 'bikes/:loggable_id/logs' => 'bike_logs#create' - # See how all your routes lay out with "rake routes" + #match ':loggable_type/:loggable_id/logs' => 'acts_as_loggable/logs#index', :as => 'loggable_logs' - # This is a legacy wild controller route that's not recommended for RESTful applications. - # Note: This route will make all actions in every controller accessible via GET requests. - # match ':controller(/:action(/:id))(.:format)' + root :to => 'site#index' end