diff --git a/app/controllers/admin/web_resources_controller.rb b/app/controllers/admin/web_resources_controller.rb index ab03f6a..a37345d 100644 --- a/app/controllers/admin/web_resources_controller.rb +++ b/app/controllers/admin/web_resources_controller.rb @@ -6,7 +6,7 @@ class Admin::WebResourcesController < OrbitAdminController @tags = @module_app.tags @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) - @table_fields = [:status, :category, :title] + @table_fields = [:status, :category, :title, "ad_banner.duration", "click_count"] if params[:sort].blank? s = {:order_position => "asc"} else diff --git a/app/controllers/web_resources_controller.rb b/app/controllers/web_resources_controller.rb index decb14c..8ee4d69 100644 --- a/app/controllers/web_resources_controller.rb +++ b/app/controllers/web_resources_controller.rb @@ -1,5 +1,6 @@ class WebResourcesController < ApplicationController require 'nokogiri' + skip_before_action :verify_authenticity_token, only: [:track_click] def index OrbitHelper.render_css_in_head(["web_resources_front"]) links = WebLink.where(:title.ne => "").can_display.filter_by_categories.filter_by_tags @@ -33,7 +34,8 @@ class WebResourcesController < ApplicationController "statuses" => statuses, "category" => link.category.title, "link_to_show" => link.url, - "target" => target + "target" => target, + "onclick" => "if(navigator.sendBeacon){navigator.sendBeacon('/web_resources/track_click/#{link.id}');}" } end { @@ -79,7 +81,8 @@ class WebResourcesController < ApplicationController "context" => nl2br(link.context.to_s), "statuses" => statuses, "link_to_show" => link.url, - "target" => target + "target" => target, + "onclick" => "if(navigator.sendBeacon){navigator.sendBeacon('/web_resources/track_click/#{link.id}');}" } end { @@ -87,4 +90,12 @@ class WebResourcesController < ApplicationController "extras" => {"widget-title"=>t(:web_resource),"more_url" => OrbitHelper.widget_more_url} } end + + def track_click + link = WebLink.find(params[:id]) + link.inc(click_count: 1) + head :ok + rescue Mongoid::Errors::DocumentNotFound + head :not_found + end end diff --git a/app/models/web_link.rb b/app/models/web_link.rb index 375e02d..b656ee2 100644 --- a/app/models/web_link.rb +++ b/app/models/web_link.rb @@ -18,12 +18,15 @@ class WebLink field :create_user_id field :update_user_id field :rss2_id, type: String + field :click_count, type: Integer, default: 0 + field :postdate, type: DateTime, default: Time.now + field :deadline, type: DateTime field :link_open, type: String LINK_OPEN_TYPES = ["local", "new_window"] - scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:order_position,:asc])} - + scope :can_display, ->{where(is_hidden: false).and(WebLink.unscoped.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,WebLink.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector).order_by([:is_top,:desc],[:order_position,:asc])} + before_save :add_http protected @@ -41,4 +44,4 @@ class WebLink self.url_translations = temp_url end -end \ No newline at end of file +end diff --git a/app/views/admin/web_resources/_form.html.erb b/app/views/admin/web_resources/_form.html.erb index 93bceff..2df9ce9 100644 --- a/app/views/admin/web_resources/_form.html.erb +++ b/app/views/admin/web_resources/_form.html.erb @@ -33,6 +33,22 @@ <%= select_category(f, @module_app) %> + + +