Compare commits

...

4 Commits

13 changed files with 208 additions and 106 deletions

11
Gemfile
View File

@ -84,8 +84,11 @@ end
#ask #ask
gem 'gotcha' gem 'gotcha'
#desktop #observers
gem 'angularjs-rails', '~> 1.2.20' gem 'mongoid-observers'
gem 'angular-ui-bootstrap-rails', '~> 0.11.0'
gem 'jquery_mousewheel_rails', '~> 3.1.11.3' #desktop
# gem 'angularjs-rails', '~> 1.2.20'
# gem 'angular-ui-bootstrap-rails', '~> 0.11.0'
# gem 'jquery_mousewheel_rails', '~> 3.1.11.3'

View File

@ -27,7 +27,10 @@ class PagesController < ApplicationController
def home def home
@manifest = @key @manifest = @key
page = Page.first page = Page.first
Thread.new do
impressionist(page) impressionist(page)
page.inc(view_count: 1)
end
OrbitHelper.set_params params,current_user OrbitHelper.set_params params,current_user
OrbitHelper.set_site_locale locale OrbitHelper.set_site_locale locale
render :html => render_final_page("home",page,true).html_safe render :html => render_final_page("home",page,true).html_safe
@ -110,7 +113,10 @@ class PagesController < ApplicationController
layout = true layout = true
end end
Thread.new do
impressionist(page) impressionist(page)
page.inc(view_count: 1)
end
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe
else else
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found 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) def render_final_page(original_view=get_view,page,layout)
final_html_for_render = "" final_html_for_render = ""
if layout if layout
parts = $mobile.blank? ? (page.page_parts rescue []) : (page.mobile_page_parts rescue []) parts = $mobile.blank? ? (page.page_parts rescue []) : (page.mobile_page_parts rescue [])
@part_partials = {} @part_partials = {}
parts.each do |part| parts.each do |part|
subparts = part.sub_parts.asc(:created_at) subparts = part.sub_parts.asc(:created_at)
partials = [] partials = []
@ -326,7 +332,10 @@ class PagesController < ApplicationController
if @editmode 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>" 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 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,{ race_condition_ttl: 2, expires_in: 5.minutes}) do
render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type,subpart.id.to_s)
end
partials << widget_html
end end
elsif subpart.kind == "text" elsif subpart.kind == "text"
if @editmode if @editmode

View File

@ -76,6 +76,7 @@ module ApplicationHelper
end end
def render_menu 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")) # json_file = File.read(File.join(Rails.root, 'public', "menu.json"))
# @items = JSON.parse(json_file) # @items = JSON.parse(json_file)
if $mobile.blank? if $mobile.blank?
@ -166,6 +167,9 @@ module ApplicationHelper
h.html_safe h.html_safe
end end
menu_html
end
def render_view def render_view
def render_link_to_edit(html, url_to_edit) def render_link_to_edit(html, url_to_edit)
@ -269,6 +273,7 @@ module ApplicationHelper
doc.to_html.html_safe doc.to_html.html_safe
else else
unless data['impressionist'].blank? unless data['impressionist'].blank?
Thread.new do
impression = data['impressionist'].impressions.create impression = data['impressionist'].impressions.create
impression.user_id = request.session['user_id'] impression.user_id = request.session['user_id']
impression.controller_name = params[:target_controller] impression.controller_name = params[:target_controller]
@ -278,8 +283,8 @@ module ApplicationHelper
impression.request_hash = @impressionist_hash impression.request_hash = @impressionist_hash
impression.referrer = request.referrer impression.referrer = request.referrer
impression.save impression.save
data['impressionist'].view_count = data['impressionist'].impressions.count data['impressionist'].inc(view_count: 1)
data['impressionist'].save end
end end
wrap_elements = doc.css("*[data-list][data-level='0']") wrap_elements = doc.css("*[data-list][data-level='0']")
if wrap_elements.count == 0 if wrap_elements.count == 0

View File

@ -102,23 +102,29 @@ module OrbitBackendHelper
end end
def display_visitors(options={}) def display_visitors(options={})
Impression.where(options).distinct(:request_hash).count Impression.where(options).count
end end
def display_visitors_today 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 end
def display_visitors_this_week 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 end
def display_visitors_this_month 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 end
def display_visitors_this_year 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 end
def get_month_traffic def get_month_traffic

View File

@ -15,6 +15,7 @@ class ModuleApp
field :desktop_enabled, type: Boolean, default: false field :desktop_enabled, type: Boolean, default: false
field :widget_settings field :widget_settings
field :store_permission_granted, type: Boolean, default: false field :store_permission_granted, type: Boolean, default: false
field :cache_models, type: Array, default: []
has_many :categories, dependent: :destroy, :autosave => true has_many :categories, dependent: :destroy, :autosave => true
has_and_belongs_to_many :tags, dependent: :destroy, :autosave => true has_and_belongs_to_many :tags, dependent: :destroy, :autosave => true
@ -34,6 +35,7 @@ class ModuleApp
self[:widget_methods] = reg.get_widget_methods self[:widget_methods] = reg.get_widget_methods
self[:widget_settings] = reg.get_widget_settings self[:widget_settings] = reg.get_widget_settings
self[:desktop_enabled] = reg.is_desktop_enabled self[:desktop_enabled] = reg.is_desktop_enabled
self[:cache_models] = reg.get_models_to_cache
end end
def sub_managers def sub_managers

View File

@ -0,0 +1,26 @@
class OrbitObserver < Mongoid::Observer
models_to_cache = OrbitApp.get_models_for_caching
observe models_to_cache.keys
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_hash_for_caching
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.name.underscore.to_sym]}/ )
end
end
end

View File

@ -32,6 +32,15 @@ class Page
before_create :assign_page_number 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 def assign_page_number
parent_page = self.parent_page parent_page = self.parent_page
if !parent_page.nil? if !parent_page.nil?

View File

@ -16,4 +16,11 @@ class SubPart
belongs_to :page_part belongs_to :page_part
belongs_to :mobile_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 end

View File

@ -37,5 +37,7 @@ module Orbit
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # 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.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de # config.i18n.default_locale = :de
config.mongoid.observers = :orbit_observer
end end
end end

View File

@ -0,0 +1,7 @@
GEM
specs:
PLATFORMS
ruby
DEPENDENCIES

View File

@ -40,7 +40,7 @@ module OrbitApp
end end
class RegisteredModule class RegisteredModule
attr_reader :name,:key,:base_path, :module_label,:widget_methods,:authorizable_models,:is_authorizable, :data_count, :widget_settings, :icon_class_no_sidebar,:desktop_enabled attr_reader :name,:key,:base_path, :module_label,:widget_methods,:authorizable_models,:is_authorizable, :data_count, :widget_settings, :icon_class_no_sidebar,:desktop_enabled, :models_to_cache
def initialize(name,&block) def initialize(name,&block)
@name = name @name = name
@ -48,6 +48,7 @@ module OrbitApp
@side_bar = nil @side_bar = nil
@module_label = @name @module_label = @name
@widget_methods = [] @widget_methods = []
@models_to_cache = []
@widget_settings = {} @widget_settings = {}
@is_taggable = false @is_taggable = false
@authorizable_models = [] @authorizable_models = []
@ -93,6 +94,14 @@ module OrbitApp
@widget_settings = settings.first @widget_settings = settings.first
end end
def models_to_cache(models)
@models_to_cache = models
end
def get_models_to_cache
@models_to_cache
end
def get_widget_settings def get_widget_settings
@widget_settings @widget_settings
end end

View File

@ -21,6 +21,23 @@ module OrbitApp
end end
end end
def get_models_for_caching
@models_for_caching = {}
ModuleApp.all.each do |ma|
# models.concat(ma.cache_models)
ma.cache_models.each do |cm|
@models_for_caching[cm] = ma.key
end
end
@models_for_caching[:tag] = "Tag"
@models_for_caching[:category] = "Category"
return @models_for_caching
end
def get_model_hash_for_caching
@models_for_caching
end
def check_module_permissions def check_module_permissions
store_token = Site.first.store_token rescue nil store_token = Site.first.store_token rescue nil
module_apps = ModuleApp.all module_apps = ModuleApp.all

View File

@ -4,7 +4,7 @@ module OrbitModel
include Impressionist::Impressionable include Impressionist::Impressionable
base.extend ClassMethods 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 base.field :view_count, :type => Integer, :default => 0
end end