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



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

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



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

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 to 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.



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

def dc_find_form_file(form_file)
  form_path=nil
  if form_file.match(/\.|\//)
    form_path,form_file=form_file.split(/\.|\//)
  end
  DrgCms.paths(:forms).reverse.each do |path|
    f = "#{path}/#{form_file}.yml"
    return f if File.exist?(f) and (form_path.nil? or path.to_s.match(/\/#{form_path}\//i))
  end
  p "Form file #{form_file} not found!"
  nil
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,



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

def dc_get_site()
  return @site if @site 
  uri  = URI.parse(request.url)
  @site = DcSite.find_by(name: uri.host)
# Site can be aliased
  if @site and !@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? and 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 and !@site.active # site is disabled
  @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: path=#{params[:path]} site=#{@site.name if @site} page=#{@page.subject if @page} design=#{@design}")
  render(file: "#{Rails.root}/public/404", :status => 404, :layout => false, :formats => [:html])
end

#dc_user_has_role(role) ⇒ Boolean

Checks if user has required role.

Examples:

If user has required role

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

Parameters:

  • role (DcPolicyRole)

    can be passed as DcPolicyRole object or

  • role (String)

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

Returns:

  • (Boolean)

    True if user has required role added to his profile.



67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/dc_application_controller.rb', line 67

def dc_user_has_role(role)
  if role.class == String
    rol = role
    role = DcPolicyRole.find_by(name: rol)
    role = DcPolicyRole.find_by(system_name: rol) if role.nil?
  end
  return false if role.nil? or session[:user_roles].nil?
# role is found 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.



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

def set_page_title()
  @page_title = @page.title.blank? ? "#{@page.subject} - #{@site.page_title}" : @page.title
end