Class: DcApplicationController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/dc_application_controller.rb

Overview

DcApplicationControllerController holds methods which are useful for all application controllers.

Instance Method Summary collapse

Instance Method Details

#dc_dump(*args) ⇒ Object

Writes anything passed as parameter to logger file. Very useful for debuging strange errors.

Parameters:

  • args (Objects)

    any parameter can be passed



39
40
41
42
43
# File 'app/controllers/dc_application_controller.rb', line 39

def dc_dump(*args)
  args.each do |arg|
    logger.debug arg.to_s
  end
end

#dc_edit_mode?Boolean

Return true if CMS is in edit mode

Returns:

  • (Boolean)

    True if user CMS edit mode is selected



50
51
52
# File 'app/controllers/dc_application_controller.rb', line 50

def dc_edit_mode?
  session[:edit_mode] > 1
end

#dc_find_form_file(form_file) ⇒ String

Searches forms path for file_name and returns full file name or nil if not found.

be useful when you are extending form but want to retain same name as original form For example. You are extending dc_user form from drg_cms gem and want to retain same dc_user name. This can be done by setting drg_cms.dc_user as extend option.

Parameters:

  • Form (String)

    file name. File name can be passed as gem_name.filename. This can

Returns:

  • (String)

    Form file name including path or nil if not found.



127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/dc_application_controller.rb', line 127

def dc_find_form_file(form_file)
  form_path = nil
  form_path, form_file = form_file.split(/\.|\//) if form_file.match(/\.|\//)

  DrgCms.paths(:forms).reverse.each do |path|
    f = "#{path}/#{form_file}.yml"
    return f if File.exist?(f) && (form_path.nil? || path.to_s.match(/\/#{form_path}(-|\/)/i))
  end
  raise "Exception: Form file '#{form_file}' not found!"
end

#dc_get_siteDcSite

Determines site from url and returns site document.

‘test’ document is returned. If site has alias set then alias site document is returned.

Examples:

Returns Google analytics code from site settings

settings = dc_get_site.params['ga_acc']

Returns:

  • (DcSite)

    site document. If site is not found and not in production environment,



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/dc_application_controller.rb', line 83

def dc_get_site
  return @site if @site

  uri  = URI.parse(request.url)
  cache_key = ['dc_site', uri.host]

  @site = dc_cache_read(cache_key)
  return @site if @site

  @site = DcSite.find_by(name: uri.host)
  # Site can be aliased
  if @site && !@site.alias_for.blank?
    @site = DcSite.find_by(name: @site.alias_for)
  end
  # Development environment. Check if site with name test exists and use
  # alias_for as pointer to real site.
  if @site.nil? && ENV["RAILS_ENV"] != 'production'
    @site = DcSite.find_by(name: 'test')
    @site = DcSite.find_by(name: @site.alias_for) if @site
  end
  @site = nil if @site && !@site.active # site is disabled
  dc_cache_write(cache_key, @site)
end

#dc_log_visitObject

Will write document to dc_visits collection unless visit comes from robot. It also sets session variable to true if robot.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/dc_application_controller.rb', line 156

def dc_log_visit
  if request.env["HTTP_USER_AGENT"] and request.env["HTTP_USER_AGENT"].match(/\(.*https?:\/\/.*\)/)
    logger.info "ROBOT: #{Time.now.strftime('%Y.%m.%d %H:%M:%S')} id=#{@page.id} ip=#{request.remote_ip}."
    session[:is_robot] = true
  else
    DcVisit.create(site_id: @site.id, 
                   user_id: session[:user_id], 
                   page_id: @page.id, 
                   ip: request.remote_ip,
                   session_id: request.session_options[:id],
                   time: Time.now )
  end
end

#dc_render_404(where_the_error_is = nil) ⇒ Object

Will render public/404.html file with some debug code includded.

Examples:

Render error

site = dc_get_site()
return dc_render_404('Site') unless site

Parameters:

  • Object (Object)

    where_the_error_is. Additional data can be displayed with error.



147
148
149
150
# File 'app/controllers/dc_application_controller.rb', line 147

def dc_render_404(where_the_error_is=nil)
  logger.info("Error 404;#{request.env['REQUEST_URI'] rescue ''};#{request.referer};#{where_the_error_is}")
  render(file: Rails.root.join('public/404.html'), status: 404)
end

#dc_user_has_role(role) ⇒ Boolean

Checks if user has required role.

as role name. If passed as name, dc_policy_roles is searched for appropriate role.

Examples:

If user has required role

if dc_user_has_role('admin') ...
if dc_user_has_role('Site editors') ...

Parameters:

  • role (DcPolicyRole or String)

    can be passed as DcPolicyRole object or

Returns:

  • (Boolean)

    True if user has required role added to his profile.



66
67
68
69
70
71
# File 'app/controllers/dc_application_controller.rb', line 66

def dc_user_has_role(role)
  role = DcPolicyRole.get_role(role)
  return false if role.nil? || session[:user_roles].nil?
  # role exists in user_roles
  session[:user_roles].include?(role._id)
end

#set_page_titleObject

Will set page title according to data on dc_page or dc_site

Sets internal @page_title variable.



112
113
114
115
# File 'app/controllers/dc_application_controller.rb', line 112

def set_page_title
  @page_title = @page.title.blank? ? @page.subject : @page.title
  dc_add_meta_tag(:name, 'description', @page.meta_description)
end