Class: AgileApplicationController

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

Overview

AgileApplicationController holds methods which are useful for all application controllers.

Instance Method Summary collapse

Instance Method Details

#agile_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



38
39
40
41
42
# File 'app/controllers/agile_application_controller.rb', line 38

def agile_dump(*args)
  args.each do |arg|
    logger.info arg.to_s
  end
end

#agile_edit_mode?Boolean

Return true if CMS is in edit mode

Returns:

  • (Boolean)

    True if user CMS edit mode is selected



49
50
51
# File 'app/controllers/agile_application_controller.rb', line 49

def agile_edit_mode?
  session[:edit_mode].to_i > 1
end

#agile_get_siteArSite

Determines site from url and returns site record.

‘development’ record is returned. If site record has alias set then alias site record is returned.

Examples:

Returns Google analytics code from site settings

settings =  agile_get_site.params['ga_acc']

Returns:

  • (ArSite)

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



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

def agile_get_site
  return @site if @site

  uri = URI.parse(request.url)
  cache_key = ['ar_site', uri.host]
  @site = Agile.cache_read(cache_key)
  return @site if @site

  @site = ArSite.find_by(name: uri.host)
  # Site can be aliased

  if @site&.alias_for.present?
    @site = ArSite.find_by(name: @site.alias_for)
  end
  # Development environment. Check if site with name development exists and use

  # alias_for as pointer to our site.

  if @site.nil? && !Rails.env.production?
    @site = ArSite.find_by(name: 'development')
    @site = ArSite.find_by(name: @site.alias_for) if @site
  end
  @site = nil unless @site&.active # might be disabled

  Agile.cache_write(cache_key, @site)
end

#agile_process_default_requestObject

This is default page process action. It will search for site, page and design records, collect parameters from different objects, add CMS edit code if allowed and at the end render design.body or design.rails_view or site.rails_view.

Examples:

as defined in routes.rb

get '*path' => 'agileapplication_controller#agileprocess_default_request'
# or
get '*path' => 'my_controller#page'
# then in my_controller.rb
def page
  agileprocess_default_request
end


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/controllers/agile_application_controller.rb', line 161

def agile_process_default_request
  session[:edit_mode] ||= 0
  # Initialize parts

  @parts    = nil
  @js, @css = '', ''
  # find domain name in sites

  @site =  agile_get_site
  # site not defined. render 404 error

  return agile_render_404('Site!') if @site.nil?

  agile_options_set(@site.settings)
  # HOMEPAGE. When no parameters is set

  params[:path]   = @site.homepage_link if params[:id].nil? && params[:path].nil?
  @options[:path] = params[:path].to_s.downcase.split('/')
  params[:path]   = @options[:path].first if @options[:path].size > 1
  # some other process request. It should fail if not defined

  return send(@site.request_processor) unless @site.request_processor.blank?

  # Search for page

  page_class = @site.page_klass
  if params[:id]
    @page = page_class.find_by(ar_site_id: [@site.id, nil], link: params[:id], active: true)
    @page = page_class.find(params[:id]) if @page.nil? # there will be more link searchers than by id

  elsif params[:path]
    # path may point direct to page's link

    @page = page_class.find_by( :ar_site_id => [@site.id, nil], link: params[:path], active: true)
    if @page.nil?
      # no. Find if defined in links

      if (link = ArLink.find_by( :ar_site_id => [@site.id, nil], link: params[:path]))
        if link.page_id
          agile_options_set link.params
          @page = page_class.find(link.page_id)
        else
          redirect_to(link.redirect, allow_other_host: true) and return
        end
      end
    end
  end
  # if @page is not found render 404 error

  return  agile_render_404('Page!') unless @page

  agile_mobile_set unless session[:is_mobile] # do it only once per session

  # find design if defined. Otherwise design MUST be declared in site

  if @page.ar_design_id
    @design = ArDesign.find(@page.ar_design_id)
    return agile_render_404('Design!') unless @design
  end
  agile_options_set(@design.params) if @design
  agile_options_set(@page.params)
  #agile_add_json_ld(@page.get_json_ld)

  # Add edit menu

  if session[:edit_mode] > 0
    session[:site_id]         = @site.id
    session[:site_page_class] = @site.page_class
    session[:page_id]         = @page.id
  else
    # Log visits from non-editors

    #agile_visit_log()

  end
  set_page_title()
  agile_render_design(@design)
end

#agile_render_404(where_the_error_is = nil) ⇒ Object

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

Examples:

Render error

site =  agile_get_site()
return  agile_render_404('Site') unless site

Parameters:

  • Object (Object)

    where_the_error_is. Additional data can be displayed with error.



124
125
126
127
# File 'app/controllers/agile_application_controller.rb', line 124

def agile_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

#agile_user_has_role?(role) ⇒ Boolean

Checks if user has required role.

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

Examples:

If user has required role

if agile_user_has_role?('admin') ...
if agile_user_has_role?('Site editors') ...

Parameters:

  • role (ArRole or String)

    can be passed as ArRole object or

Returns:

  • (Boolean)

    True if user has required role added to his profile.



65
66
67
68
69
70
# File 'app/controllers/agile_application_controller.rb', line 65

def agile_user_has_role?(role)
  role = ArRole.get_role(role)
  return false if role.nil? || session[:user_roles].nil?
  # role exists in user_roles

  session[:user_roles].include?(role.id)
end

#agile_visit_logObject

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



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/agile_application_controller.rb', line 133

def agile_visit_log
  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
    ArVisit.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

#set_page_titleObject

Will set page title according to data on ar_page or ar_site

Sets internal @page_title variable.



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

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