diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index d7efbdb..e651d29 100755 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -5,7 +5,23 @@ class AdminController < ApplicationController def dashboard end - + + def analytics + + if params[:field].nil? + fields = "*" + else + fields = params[:field].map {|k,v| k}.join(",") + end + + if params[:ip] + @analytics = Analytics.hits_by_ip(params[:ip], fields) + else + @analytics = Analytics.all + end + render "layouts/admin/_analytics" + end + def get_all_users @users = User.all render :partial => "layouts/admin/get_all_users" diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 86c33e2..3e56186 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,6 @@ class ApplicationController < ActionController::Base - before_filter :authenticated, :has_info + before_filter :authenticated, :has_info, :create_analytic helper_method :current_user, :is_admin?, :sanitize_font # Our security guy keep talking about sea-surfing, cool story bro. @@ -45,6 +45,10 @@ class ApplicationController < ActionController::Base redirect_to home_dashboard_index_path if redirect end + def create_analytic + Analytics.create({ :ip_address => request.remote_ip, :referrer => request.referrer, :user_agent => request.user_agent}) + end + def sanitize_font(css) css # css if css.match(/\A[0-9]+([\%]|pt)\z/) diff --git a/app/models/analytics.rb b/app/models/analytics.rb new file mode 100644 index 0000000..c3c6075 --- /dev/null +++ b/app/models/analytics.rb @@ -0,0 +1,9 @@ +class Analytics < ActiveRecord::Base + attr_accessible :ip_address, :referrer, :user_agent + + scope :hits_by_ip, ->(ip,col="*") { select("#{col}").where("ip_address = '#{ip}'")} + + def self.count_by_col(col) + calculate(:count, col) + end +end diff --git a/app/views/layouts/admin/_analytics.html.erb b/app/views/layouts/admin/_analytics.html.erb new file mode 100644 index 0000000..485c2b0 --- /dev/null +++ b/app/views/layouts/admin/_analytics.html.erb @@ -0,0 +1,44 @@ +
+ Search by IP:
+ IP Address
+ Referrer
+ User Agent +
+ +
+ + + + <% params[:field].count.times do %> + + <% end %> + + + + <% @analytics.each do |a|%> + + <% a.attributes.each do |k,v| %> + + <% end %> + + <% end %> + +
 
<%= v %>
+ +
+
+
+ + + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9386fd0..df7bf2d 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,6 +82,7 @@ Railsgoat::Application.routes.draw do post "delete_user" put "update_user" get "get_all_users" + get "analytics" end resources :dashboard do diff --git a/db/migrate/20140408185601_create_analytics.rb b/db/migrate/20140408185601_create_analytics.rb new file mode 100644 index 0000000..459db9d --- /dev/null +++ b/db/migrate/20140408185601_create_analytics.rb @@ -0,0 +1,10 @@ +class CreateAnalytics < ActiveRecord::Migration + def change + create_table :analytics do |t| + t.string :ip_address + t.string :referrer + t.string :user_agent + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index a32189b..a51d0db 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,15 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140315002730) do +ActiveRecord::Schema.define(:version => 20140408185601) do + + create_table "analytics", :force => true do |t| + t.string "ip_address" + t.string "referrer" + t.string "user_agent" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end create_table "benefits", :force => true do |t| t.datetime "created_at", :null => false