Class: DcApplicationController

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

Overview

Controller holds methods which are of use for all application controllers.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dc_get_site_Object

Determine and return site record from url. It would be nice but it is not working.



104
105
106
# File 'app/controllers/dc_application_controller.rb', line 104

def self.dc_get_site_() #:nodoc:
  #self.dc_get_site()
end

Instance Method Details

#dc_dump(*args) ⇒ Object

Writes anything passed as parameter to logger file.

Very usefull for debuging strange errors.



36
37
38
39
40
# File 'app/controllers/dc_application_controller.rb', line 36

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)


45
46
47
# File 'app/controllers/dc_application_controller.rb', line 45

def dc_edit_mode?
  session[:edit_mode] > 1
end

#dc_find_form_file(form_file) ⇒ Object

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

Parameters:
form_file

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

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.

Return:

String. Full form file name or nil if not found.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/dc_application_controller.rb', line 120

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_siteObject

Determine site from url and return site document.

Return:

Site document. If site is not found and not in production environment ‘test’ site document is returned. If site has alias set then alias site document is returned.



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

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.



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/controllers/dc_application_controller.rb', line 148

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.

Parameters:
Object where_the_error_is

Additional data can be displayed with error.



139
140
141
142
# File 'app/controllers/dc_application_controller.rb', line 139

def dc_render_404(where_the_error_is=nil)
  render(file: "#{Rails.root}/public/404", :status => 404, :layout => false, :formats => [:html], 
         locals: {error_is: where_the_error_is})
end

#dc_user_has_role(role) ⇒ Object

Checks if user has required role.

Parameters:
role

Role can be passed as DcPolicyRole object or as string. If string is passed, dc_policy_roles files is searched for appropriate role.

Return:

Boolean. True if user has required role added to his profile.

Example:

if dc_user_has_role(‘admin’) … if dc_user_has_role(‘Site editors’) …



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

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?
# role is found in user_roles
  session[:user_roles].include?(role._id)
end