forked from saurabh/orbit4-5
update impression and add cache for page sub-parts, main-menu
This commit is contained in:
parent
f52919e4b6
commit
48f50f6ebe
|
@ -27,7 +27,10 @@ class PagesController < ApplicationController
|
|||
def home
|
||||
@manifest = @key
|
||||
page = Page.first
|
||||
Thread.new do
|
||||
impressionist(page)
|
||||
page.inc(view_count: 1)
|
||||
end
|
||||
OrbitHelper.set_params params,current_user
|
||||
OrbitHelper.set_site_locale locale
|
||||
render :html => render_final_page("home",page,true).html_safe
|
||||
|
@ -110,7 +113,10 @@ class PagesController < ApplicationController
|
|||
layout = true
|
||||
end
|
||||
|
||||
Thread.new do
|
||||
impressionist(page)
|
||||
page.inc(view_count: 1)
|
||||
end
|
||||
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe
|
||||
else
|
||||
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
|
||||
|
@ -301,11 +307,11 @@ class PagesController < ApplicationController
|
|||
|
||||
def render_final_page(original_view=get_view,page,layout)
|
||||
final_html_for_render = ""
|
||||
|
||||
if layout
|
||||
parts = $mobile.blank? ? (page.page_parts rescue []) : (page.mobile_page_parts rescue [])
|
||||
|
||||
@part_partials = {}
|
||||
|
||||
parts.each do |part|
|
||||
subparts = part.sub_parts.asc(:created_at)
|
||||
partials = []
|
||||
|
@ -326,7 +332,10 @@ class PagesController < ApplicationController
|
|||
if @editmode
|
||||
partials << "<div class='editmode-ps' title='#{subpart.module}'> " + render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type,subpart.id.to_s) + "<a href='/page_parts/edit_sub_part?page_id=#{page.id.to_s}&part_id=#{part.id.to_s}&sub_part_id=#{subpart.id.to_s}#{(!$mobile.blank? ? '&mobile_view=1' : '')}'> </a></div>"
|
||||
else
|
||||
partials << render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type,subpart.id.to_s)
|
||||
widget_html = Rails.cache.fetch("subpart_#{subpart.module}_#{subpart.id.to_s}_"+I18n.locale.to_s) do
|
||||
render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type,subpart.id.to_s)
|
||||
end
|
||||
partials << widget_html
|
||||
end
|
||||
elsif subpart.kind == "text"
|
||||
if @editmode
|
||||
|
|
|
@ -76,6 +76,7 @@ module ApplicationHelper
|
|||
end
|
||||
|
||||
def render_menu
|
||||
menu_html = Rails.cache.fetch(['main_menu',request.original_fullpath, I18n.locale], race_condition_ttl: 2.seconds) do
|
||||
# json_file = File.read(File.join(Rails.root, 'public', "menu.json"))
|
||||
# @items = JSON.parse(json_file)
|
||||
if $mobile.blank?
|
||||
|
@ -166,6 +167,9 @@ module ApplicationHelper
|
|||
h.html_safe
|
||||
end
|
||||
|
||||
menu_html
|
||||
end
|
||||
|
||||
def render_view
|
||||
|
||||
def render_link_to_edit(html, url_to_edit)
|
||||
|
@ -269,6 +273,7 @@ module ApplicationHelper
|
|||
doc.to_html.html_safe
|
||||
else
|
||||
unless data['impressionist'].blank?
|
||||
Thread.new do
|
||||
impression = data['impressionist'].impressions.create
|
||||
impression.user_id = request.session['user_id']
|
||||
impression.controller_name = params[:target_controller]
|
||||
|
@ -278,8 +283,8 @@ module ApplicationHelper
|
|||
impression.request_hash = @impressionist_hash
|
||||
impression.referrer = request.referrer
|
||||
impression.save
|
||||
data['impressionist'].view_count = data['impressionist'].impressions.count
|
||||
data['impressionist'].save
|
||||
data['impressionist'].inc(view_count: 1)
|
||||
end
|
||||
end
|
||||
wrap_elements = doc.css("*[data-list][data-level='0']")
|
||||
if wrap_elements.count == 0
|
||||
|
|
|
@ -102,23 +102,29 @@ module OrbitBackendHelper
|
|||
end
|
||||
|
||||
def display_visitors(options={})
|
||||
Impression.where(options).distinct(:request_hash).count
|
||||
Impression.where(options).count
|
||||
end
|
||||
|
||||
def display_visitors_today
|
||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_day, '$lte' => Time.now})
|
||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_day})
|
||||
end
|
||||
|
||||
def display_visitors_this_week
|
||||
display_visitors(created_at: {'$gte' => Time.now-7.days, '$lte' => Time.now})
|
||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_week})
|
||||
end
|
||||
|
||||
def display_visitors_this_month
|
||||
display_visitors(created_at: {'$gte' => Time.now-30.days, '$lte' => Time.now})
|
||||
visitors_this_month = Rails.cache.fetch("visitors_this_month", expires_in: 1.day) do
|
||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_month})
|
||||
end
|
||||
visitors_this_month
|
||||
end
|
||||
|
||||
def display_visitors_this_year
|
||||
display_visitors(created_at: {'$gte' => Time.now-365.days, '$lte' => Time.now})
|
||||
visitors_this_year = Rails.cache.fetch("visitors_this_year", expires_in: 1.day) do
|
||||
display_visitors(created_at: {'$gte' => Time.now.beginning_of_year})
|
||||
end
|
||||
visitors_this_year
|
||||
end
|
||||
|
||||
def get_month_traffic
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
class OrbitObserver < Mongoid::Observer
|
||||
# observe OrbitApp.get_models
|
||||
observe :tag, :category
|
||||
|
||||
def after_save(document)
|
||||
clear_cache(document)
|
||||
end
|
||||
|
||||
def after_destroy(document)
|
||||
clear_cache(document)
|
||||
end
|
||||
|
||||
def clear_cache(document)
|
||||
# model_module_hash = OrbitApp.get_model_module_hash
|
||||
case document.class.to_s
|
||||
when 'Tag'
|
||||
document.module_app.each do |module_app|
|
||||
Rails.cache.delete_matched( /#{ module_app.key }/ )
|
||||
end
|
||||
when 'Category'
|
||||
Rails.cache.delete_matched( /#{ document.module_app.key }/ )
|
||||
else
|
||||
# Rails.cache.delete_matched( /#{model_module_hash[document.class.to_s]}/ )
|
||||
end
|
||||
end
|
||||
end
|
|
@ -32,6 +32,15 @@ class Page
|
|||
|
||||
before_create :assign_page_number
|
||||
|
||||
after_save :clear_cache
|
||||
after_destroy :clear_cache
|
||||
|
||||
def clear_cache
|
||||
I18n.available_locales.each do |locale|
|
||||
Rails.cache.delete_matched( /main_menu/ )
|
||||
end
|
||||
end
|
||||
|
||||
def assign_page_number
|
||||
parent_page = self.parent_page
|
||||
if !parent_page.nil?
|
||||
|
|
|
@ -16,4 +16,11 @@ class SubPart
|
|||
|
||||
belongs_to :page_part
|
||||
belongs_to :mobile_page_part
|
||||
|
||||
after_save :clear_cache
|
||||
after_destroy :clear_cache
|
||||
|
||||
def clear_cache
|
||||
Rails.cache.delete_matched( /#{self.id.to_s}/ )
|
||||
end
|
||||
end
|
|
@ -37,5 +37,7 @@ module Orbit
|
|||
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
||||
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
||||
# config.i18n.default_locale = :de
|
||||
|
||||
config.mongoid.observers = :orbit_observer
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ module OrbitModel
|
|||
include Impressionist::Impressionable
|
||||
|
||||
base.extend ClassMethods
|
||||
base.is_impressionable :counter_cache => true, :column_name => :view_count, :unique => :request_hash
|
||||
base.is_impressionable :counter_cache => false
|
||||
base.field :view_count, :type => Integer, :default => 0
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue