Module: DcApplicationHelper
- Included in:
- BrowseModelsControl, DcAdRenderer, DcBigMenuRenderer, DcCommonRenderer, DcMenuRenderer, DcPageRenderer, DcPartRenderer, DcPieceRenderer, DcPollRenderer, DcSimpleMenuRenderer
- Defined in:
- app/helpers/dc_application_helper.rb
Overview
DcApplicationHelper defines common helper methods for using with DRG CMS.
Instance Attribute Summary collapse
-
#design ⇒ Object
readonly
design document.
-
#form ⇒ Object
readonly
form object.
-
#ids ⇒ Object
readonly
ids url parameter.
-
#options ⇒ Object
readonly
options object.
-
#page ⇒ Object
readonly
page document.
-
#page_title ⇒ Object
page title.
-
#part ⇒ Object
readonly
part.
-
#parts ⇒ Object
all parts read from page, design, …
-
#site ⇒ Object
readonly
site document.
-
#tables ⇒ Object
readonly
tables url parameter.
Instance Method Summary collapse
-
#_origin ⇒ Object
When @parent is present then helper methods are called from parent class otherwise from self.
-
#dc_add2_record_cookie(hash) ⇒ Object
Will add data to record cookie.
-
#dc_big_table(key) ⇒ Object
Returns key defined in DcBigTable as array of choices for use in select fields.
-
#dc_choices4(model, name, id = '_id', options = {}) ⇒ Object
Returns choices for select input field when choices are generated from all documents in collection.
-
#dc_choices4_all_collections ⇒ Object
Returns list of all collections (tables) as array of choices for usage in select fields.
-
#dc_choices4_cmsmenu ⇒ Object
Returns choices for creating collection edit select field on CMS top menu.
-
#dc_choices4_field(model, field) ⇒ Object
Return choices for field in model if choices are defined in localization text.
-
#dc_choices4_folders_list ⇒ Object
Returns list of directories as array of choices for use in select field on folder permission form.
-
#dc_choices4_menu ⇒ Object
Returns list of choices for selection top level menu on dc_page form.
-
#dc_choices4_site_policies ⇒ Object
Return array of policies defined in a site document formated to be used as choices for select field.
- #dc_date_time(value, format) ⇒ Object
-
#dc_deprecate(msg) ⇒ Object
Writes out deprication msg.
-
#dc_dont?(what, default = false) ⇒ Boolean
Returns true if parameter has value of 0, false, no, none or -.
-
#dc_edit_mode? ⇒ Boolean
Checks if CMS is in edit mode (CMS menu bar is visible).
-
#dc_edit_title ⇒ Object
Creates title for cmsedit edit action dialog.
-
#dc_error_messages_for(doc) ⇒ Object
Returns validation error messages for the document (record) formatted for display on message div.
-
#dc_flash_messages ⇒ Object
Returns flash messages formatted for display on message div.
-
#dc_format_date_time(value, format = nil) ⇒ Object
Returns html code for displaying date/time formatted by strftime.
-
#dc_get_site ⇒ Object
Parse site name from url and return dc_site document.
-
#dc_icon4_boolean(value) ⇒ Object
Return html code for icon presenting boolean value.
-
#dc_iframe_edit(table, opts = {}) ⇒ Object
Will return html code required for load DRG form into iframe.
-
#dc_label_for(input_html, label) ⇒ Object
Formats label and html input code for display on edit form.
-
#dc_limit_string(string, size) ⇒ Object
Truncates string length maximal to the size required and takes care, that words are not broken in middle.
-
#dc_link_for_create(opts) ⇒ Object
Will create HTML code required to create new document.
-
#dc_link_for_edit(opts) ⇒ Object
Will create HTML code required to edit document.
-
#dc_link_for_edit1(opts, link_text) ⇒ Object
Create one option in page edit link.
-
#dc_link_menu_tag(title) {|html| ... } ⇒ Object
Create edit link with edit picture.
-
#dc_link_to(caption, icon, parms, rest = {}) ⇒ Object
Similar to rails link_to, but also takes care of link icon, translation, …
-
#dc_name4_id(model, field, field_name, id = nil) ⇒ Object
Will return descriptive text for id key when field in one table (collection) has belongs_to relation to other table.
-
#dc_name4_value(model, field, value) ⇒ Object
When select field is used on form options for select can be provided by helpers.label.table_name.choices4_name locale.
-
#dc_new_title ⇒ Object
Creates title for cmsedit new action dialog.
-
#dc_page_bottom ⇒ Object
Helper for adding additional css and javascript code added by documents and renderers during page rendering.
-
#dc_page_class ⇒ Object
Return page class model defined in site document page_class field.
-
#dc_page_edit_menu(opts = @opts) ⇒ Object
Create edit menu for editing existing or creating new dc_page documents.
-
#dc_page_top ⇒ Object
Helper for rendering top CMS menu when in editing mode.
-
#dc_render(renderer, opts = {}) ⇒ Object
This is main method used for render parts of design into final HTML document.
- #dc_render_design(part) ⇒ Object
-
#dc_render_design_part(part) ⇒ Object
Used for designs with lots of common code and one (or more) part which differs.
-
#dc_render_from_site(opts = {}) ⇒ Object
Used for designs with lots of common code and one (or more) part which differs.
-
#dc_replace_in_design(opts = {}) ⇒ Object
Used for designs with lots of common code and one (or more) part which differs.
-
#dc_submit_tag(caption, icon, parms, rest = {}) ⇒ Object
Similar to rails submit_tag, but also takes care of link icon, translation, …
-
#dc_table_title(text, result_set = nil) ⇒ Object
String.
-
#dc_user_can_view(ctrl, policy_id) ⇒ Object
Will check if user roles allow user to view data in document with defined access_policy.
-
#dc_user_has_role(role, user = nil, roles = nil) ⇒ Object
Check if user has required role assigned to its user profile.
-
#decamelize_type(string) ⇒ Object
Decamelizes string.
-
#forms_merge(hash1, hash2) ⇒ Object
Merges two forms when current form extends other form.
-
#t(key, default = '') ⇒ Object
Wrapper for i18 t method, with some spice added.
-
#t_name(field_name, default = '') ⇒ Object
Returns label for field translated to current locale for usage on data entry form.
-
#t_tablename(tablename, default = nil) ⇒ Object
Returns table (collection) name translation for usage in dialog title.
Instance Attribute Details
#design ⇒ Object (readonly)
design document
35 36 37 |
# File 'app/helpers/dc_application_helper.rb', line 35 def design @design end |
#form ⇒ Object (readonly)
form object
43 44 45 |
# File 'app/helpers/dc_application_helper.rb', line 43 def form @form end |
#ids ⇒ Object (readonly)
ids url parameter
41 42 43 |
# File 'app/helpers/dc_application_helper.rb', line 41 def ids @ids end |
#options ⇒ Object (readonly)
options object
45 46 47 |
# File 'app/helpers/dc_application_helper.rb', line 45 def @options end |
#page ⇒ Object (readonly)
page document
33 34 35 |
# File 'app/helpers/dc_application_helper.rb', line 33 def page @page end |
#page_title ⇒ Object
page title
50 51 52 |
# File 'app/helpers/dc_application_helper.rb', line 50 def page_title @page_title end |
#part ⇒ Object (readonly)
part
47 48 49 |
# File 'app/helpers/dc_application_helper.rb', line 47 def part @part end |
#parts ⇒ Object
all parts read from page, design, …
52 53 54 |
# File 'app/helpers/dc_application_helper.rb', line 52 def parts @parts end |
#site ⇒ Object (readonly)
site document
37 38 39 |
# File 'app/helpers/dc_application_helper.rb', line 37 def site @site end |
#tables ⇒ Object (readonly)
tables url parameter
39 40 41 |
# File 'app/helpers/dc_application_helper.rb', line 39 def tables @tables end |
Instance Method Details
#_origin ⇒ Object
When @parent is present then helper methods are called from parent class otherwise from self.
59 60 61 |
# File 'app/helpers/dc_application_helper.rb', line 59 def _origin #:nodoc: @parent ? @parent : self end |
#dc_add2_record_cookie(hash) ⇒ Object
Will add data to record cookie. Record cookie is used to preload some data on next create action. Create action will look for cookies and if found initialize fields on form with matching name to value found in cookie data.
Example:
kukis = {'dc_page.dc_design_id' => @page.dc_design_id,
'dc_page.dc_menu_id' => @page.menu_id)
dc_add2_record_cookie(kukis)
951 952 953 954 955 956 957 958 959 |
# File 'app/helpers/dc_application_helper.rb', line 951 def (hash) kukis = if @parent.[:record] and @parent.[:record].size > 0 Marshal.load(@parent.[:record]) else {} end hash.each {|k,v| kukis[k] = v } @parent.[:record] = Marshal.dump(kukis) end |
#dc_big_table(key) ⇒ Object
Returns key defined in DcBigTable as array of choices for use in select fields. DcBigTable can be used like a key/value store for all kind of predefined values which can be linked to site and or locale.
Parameters:
- key
-
String. Key name to be searched in dc_big_tables documents.
Example:
10:
name: category
type: select
eval: dc_big_table 'categories_for_page' # as used on form
Returns: Array of choices ready for select field.
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
# File 'app/helpers/dc_application_helper.rb', line 1106 def dc_big_table(key) ret = [] bt = DcBigTable.find_by(key: key, site: dc_get_site._id, active: true) bt = DcBigTable.find_by(key: key, site: nil, active: true) if bt.nil? return ret if bt.nil? # locale = I18n.locale.to_s bt.dc_big_table_values.each do |v| # iterate each value next unless v.active desc = '' v.dc_big_table_locales.each do |l| # iterate each locale if l.locale == locale desc = l.description break end end desc = v.description if desc.blank? # get description from value description desc = v.value if desc.blank? # still blank. Use value as description ret << [desc, v.value] end ret end |
#dc_choices4(model, name, id = '_id', options = {}) ⇒ Object
Returns choices for select input field when choices are generated from all documents in collection.
Parameters:
- model
-
String. Collection (table) name in lowercase format.
- name
-
String. Field name containing description text.
- id
-
String. Field name containing id field. Default is ‘_id’
- options
-
Hash. Various options. Currently site: (:only, :with_nil, :all) is used.
Will return only documents belonging to current site, also with site not defined, or all documents.
Example (as used in forms):
50:
name: dc_poll_id
type: select
eval: dc_choices4('dc_poll','name','_id')
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
# File 'app/helpers/dc_application_helper.rb', line 908 def dc_choices4(model, name, id='_id', = {}) model = model.classify.constantize qry = model.only(id, name) if (param = [:site]) sites = [dc_get_site.id] unless param == :all sites << nil if param == :with_nil qry = qry.in(dc_site_id: sites) if sites end qry = qry.and(active: true) if model.method_defined?(:active) # qry = qry.sort(name => 1) # choices = [] # qry.each {|v| choices << [ v[name], v[id] ] } choices = qry.inject([]) {|result,e| result << [ e[name], e[id] ]} choices.sort_alphabetical_by(&:first) # use UTF-8 sort end |
#dc_choices4_all_collections ⇒ Object
Returns list of all collections (tables) as array of choices for usage in select fields. List is collected from cms_menu.yml files and may not include all collections used in application. Currently list is only used for helping defining collection names on dc_permission form.
Example (as used in forms):
form:
fields:
10:
name: table_name
type: text_with_select
eval: dc_choices4_all_collections
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 |
# File 'app/helpers/dc_application_helper.rb', line 808 def dc_choices4_all_collections choices = {} DrgCms.paths(:forms).reverse.each do |path| filename = "#{path}/cms_menu.yml" next unless File.exist?(filename) # = YAML.load_file(filename) rescue nil # load menu next if .nil? or !['menu'] # not menu or error ['menu'].each do |section| next unless section.last['items'] # next if no items section.last['items'].each do |k, v| # look for caption and key = v['table'] choices[key] ||= "#{key} - #{t(v['caption'], v['caption'])}" end end end choices.invert.to_a.sort # hash has to be inverted for values to be returned right end |
#dc_choices4_cmsmenu ⇒ Object
Returns choices for creating collection edit select field on CMS top menu.
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 |
# File 'app/helpers/dc_application_helper.rb', line 847 def () = {} DrgCms.paths(:forms).reverse.each do |path| filename = "#{path}/cms_menu.yml" next unless File.exist?(filename) = YAML.load_file(filename) rescue nil # load menu next if .nil? or !['menu'] # not menu or error = forms_merge(['menu'], ) # ignore top level part end # html = '<ul>' .to_a.sort.each do || # sort menus, result is array of sorted hashes = [1] # value is the second (1) element of array next unless ['caption'] html << "<li class=\"cmsedit-top-level-menu\">#{fa_icon(['icon'])}#{t(['caption'])}<ul>" ['items'].to_a.sort.each do |item| # as above. sort items first value = item[1] opts = { controller: value['controller'], action: value['action'], table: value['table'], formname: value['formname'] || value['table'], target: value['target'] || 'iframe_cms' } html << "<li>#{dc_link_to(t(value['caption']), value['icon'] || '', opts)}</li>" end html << '</ul></li>' end html end |
#dc_choices4_field(model, field) ⇒ Object
Return choices for field in model if choices are defined in localization text.
Parameters:
- model
-
String. Table (collection) model name (lowercase).
- field
-
String. Field name used.
Example:
dc_choices4_field('dc_user', 'state' )
Returns: Array. Choices for select input field
665 666 667 668 669 |
# File 'app/helpers/dc_application_helper.rb', line 665 def dc_choices4_field(model, field) c = t('helpers.label.' + model + '.choices4_' + field ) return ['error'] if c.match( 'translation missing' ) c.chomp.split(',').inject([]) {|r,v| r << v.split(':') } end |
#dc_choices4_folders_list ⇒ Object
Returns list of directories as array of choices for use in select field on folder permission form. Directory root is determined from dc_site.files_directory field.
881 882 883 884 885 886 887 888 |
# File 'app/helpers/dc_application_helper.rb', line 881 def dc_choices4_folders_list public = File.join(Rails.root,'public') home = File.join(public,dc_get_site.files_directory) choices = Dir.glob(home + '/**/*/').select { |fn| File.directory?(fn) } choices << home # add home choices.collect! {|e| e.gsub(public,'')} # remove public part choices.sort end |
#dc_choices4_menu ⇒ Object
Returns list of choices for selection top level menu on dc_page form. Used for defining which top level menu will be highlited when page is displayed.
Example (as used in forms):
20:
name: menu_id
type: select
eval: dc_choices4_menu
934 935 936 937 938 939 |
# File 'app/helpers/dc_application_helper.rb', line 934 def m_clas = dc_get_site. m_clas = 'DcSimpleMenu' if m_clas.blank? klass = m_clas.classify.constantize klass.(dc_get_site) end |
#dc_choices4_site_policies ⇒ Object
Return array of policies defined in a site document formated to be used as choices for select field. Method is used for selecting site policy where policy for displaying data is required.
Example (as used in forms):
name: policy_id
type: select
eval: dc_choices4_site_policies
html:
include_blank: true
790 791 792 793 |
# File 'app/helpers/dc_application_helper.rb', line 790 def dc_choices4_site_policies() site = dc_get_site() site.dc_policies.all.inject([]) { |r, policy| r << [ policy.name, policy.id] if policy.active } end |
#dc_date_time(value, format) ⇒ Object
743 744 745 746 |
# File 'app/helpers/dc_application_helper.rb', line 743 def dc_date_time(value, format) #:nodoc: dc_deprecate 'dc_date_time will be deprecated! Use dc_format_date_time instead.' dc_format_date_time(value, format) end |
#dc_deprecate(msg) ⇒ Object
Writes out deprication msg. It also adds site_name to message, so it is easier to find where the message is comming from.
67 68 69 |
# File 'app/helpers/dc_application_helper.rb', line 67 def dc_deprecate(msg) p "#{dc_get_site.name}: #{msg}" end |
#dc_dont?(what, default = false) ⇒ Boolean
Returns true if parameter has value of 0, false, no, none or -. Returns default if parameter has nil value.
Parameters:
- what
-
String/boolean/Integer.
- default
-
Default value when what has value of nil. False by default.
Example:
dc_dont?('none') # => true
dc_dont?('-') # => true
dc_dont?(1) # => false
1063 1064 1065 1066 |
# File 'app/helpers/dc_application_helper.rb', line 1063 def dc_dont?(what, default=false) return default if what.nil? %w(0 no - false none).include?(what.to_s.downcase.strip) end |
#dc_edit_mode? ⇒ Boolean
Checks if CMS is in edit mode (CMS menu bar is visible).
Returns: Boolean. True if in edit mode
419 420 421 |
# File 'app/helpers/dc_application_helper.rb', line 419 def dc_edit_mode? _origin.session[:edit_mode] > 1 end |
#dc_edit_title ⇒ Object
Creates title for cmsedit edit action dialog.
Returns: String. HTML code for title.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'app/helpers/dc_application_helper.rb', line 235 def dc_edit_title() title = @form['form']['title'] # defined as form:title:edit if title and title['edit'] and !@form['readonly'] t( title['edit'], title['edit'] ) elsif title and title['show'] and @form['readonly'] t( title['show'], title['show'] ) else # concatenate title c = (@form['readonly'] ? t('drgcms.show') : t('drgcms.edit')) + " : " c << (@form['title'] ? t( @form['title'], @form['title'] ) : t_tablename(@form['table'])) + ' : ' title = (title and title['field']) ? title['field'] : @form['form']['edit_title'] dc_deprecate('form:edit_title will be deprecated. Use form:title:field instead.') if @form['form']['edit_title'] # c << "#{@record[ title ]} : " if title and @record.respond_to?(title) c << @record._id if @record end c end |
#dc_error_messages_for(doc) ⇒ Object
Returns validation error messages for the document (record) formatted for display on message div.
Parameters:
- doc
-
Document. Document record which will be checked for errors.
Returns: String. HTML code formatted for display.
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'app/helpers/dc_application_helper.rb', line 396 def (doc) return '' unless doc.errors.any? msgs = '' doc.errors.each do |attribute, errors_array| label = t("helpers.label.#{decamelize_type(doc.class)}.#{attribute}", attribute) msgs << "<li>#{label} : #{errors_array}</li>" end c = <<eot <div class="dc-form-error"> <h2>#{t('drgcms.errors_no')} #{doc.errors.size}</h2> <ul>#{msgs}</ul> </div> eot c.html_safe end |
#dc_flash_messages ⇒ Object
Returns flash messages formatted for display on message div.
Returns: String. HTML code formatted for display.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'app/helpers/dc_application_helper.rb', line 343 def () err = _origin.flash[:error] war = _origin.flash[:warning] inf = _origin.flash[:info] note = _origin.flash[:note] unless err.nil? and war.nil? and inf.nil? and note.nil? c = '' c << "<div class=\"dc-form-error\">#{err}</div>" if err c << "<div class=\"dc-form-warning\">#{war}</div>" if war c << "<div class=\"dc-form-info\">#{inf}</div>" if inf c << note if note _origin.flash[:error] = nil _origin.flash[:warning] = nil _origin.flash[:info] = nil _origin.flash[:note] = nil c.html_safe end end |
#dc_format_date_time(value, format = nil) ⇒ Object
Returns html code for displaying date/time formatted by strftime. Will return ” if value is nil.
Parameters:
- value
-
Date/DateTime/Time.
- format
-
String. strftime format mask. Defaults to locale’s default format.
734 735 736 737 738 |
# File 'app/helpers/dc_application_helper.rb', line 734 def dc_format_date_time(value, format=nil) return '' if value.nil? format ||= value.class == Date ? t('date.formats.default') : t('time.formats.default') value.strftime(format) end |
#dc_get_site ⇒ Object
Parse site name from url and return dc_site document. Site document will be cached in If not in production environment and site document is not found method will search for ‘test’ document and return dc_site document found in alias_for field.
Returns: DCSite. Site document.
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'app/helpers/dc_application_helper.rb', line 758 def dc_get_site() return @site if @site # already cached # req = _origin.request.url # different when called from renderer uri = URI.parse(req) @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 field as pointer to real site name. 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_icon4_boolean(value) ⇒ Object
Return html code for icon presenting boolean value. Icon is a picture of checked or unchecked box.
Parameters:
- value
-
Boolean.
Example:
# usage from program
dc_icon4_boolean(some_value)
# usage from form description
columns:
10:
name: active
eval: dc_icon4_boolean
723 724 725 |
# File 'app/helpers/dc_application_helper.rb', line 723 def dc_icon4_boolean(value) dc_dont?(value, true) ? fa_icon('square-o lg') : fa_icon('check-square-o lg') end |
#dc_iframe_edit(table, opts = {}) ⇒ Object
Will return html code required for load DRG form into iframe. If parameters are passed to method iframe url will have initial value and thus enabling automatic form load on page display.
Parameters:
- table
-
String: Collection (table) name used to load initial form.
- opts
-
Hash: Optional parameters which define url for loading DRG form.
These parameters are :action, :oper, :table, :formname, :id, :readonly
Example:
# just iframe code
<%= dc_iframe_edit(nil) %>
# load note form for note collection
<%= dc_iframe_edit('note') %>
# on register collection use reg_adresses formname to display data with id @register.id
<%= dc_iframe_edit('register', action: :show, formname: 'reg_adresses', readonly: 1, id: @register.id ) %>
Returns: Html code for edit iframe
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 |
# File 'app/helpers/dc_application_helper.rb', line 1150 def dc_iframe_edit(table, opts={}) ret = if params.size > 2 and table # controller, action, path is minimal params[:controller] = 'cmsedit' params[:action] = (params[:oper] and (params[:oper] == 'edit')) ? 'edit' : 'index' params[:action] = opts[:action] unless params[:oper] params[:table] ||= table params[:formname] ||= opts[:formname] || table params[:id] ||= params[:idp] || opts[:id] params[:readonly] ||= opts[:readonly] params[:path] = nil params.permit! # rails 5 request "<iframe id='iframe_edit' name='iframe_edit' src='#{url_for params}'></iframe>" else "<iframe id='iframe_edit' name='iframe_edit'></iframe>" end ret.html_safe end |
#dc_label_for(input_html, label) ⇒ Object
Formats label and html input code for display on edit form.
Parameters:
[input_html] String. HTML code for data input field.
[label] String. Input field label.
282 283 284 285 286 287 288 289 290 |
# File 'app/helpers/dc_application_helper.rb', line 282 def dc_label_for(input_html, label) c =<<eot <tr> <td class="dc-edit-label">#{label}</td> <td class="dc-edit-field">#{input_html}</td> </tr> eot c.html_safe end |
#dc_limit_string(string, size) ⇒ Object
Truncates string length maximal to the size required and takes care, that words are not broken in middle. Used for output text summary with texts that can be longer then allowed space.
Parameters:
- string
-
String of any size.
- size
-
Maximal size of the string to be returned.
Example:
dc_limit_string(description, 100)
Returns: String, truncated to required size. If string is truncated ‘…’ will be added to the end.
1082 1083 1084 1085 1086 1087 |
# File 'app/helpers/dc_application_helper.rb', line 1082 def dc_limit_string(string, size) return string if string.size < size string = string[0,size] string.chop! until (string[-1,1] == ' ' or string == '') string << '...' end |
#dc_link_for_create(opts) ⇒ Object
Will create HTML code required to create new document.
Parameters:
- opts
-
Hash. Optional parameters for url_for helper. These options must provide at least table and formname
parameters.
Example:
if @opts[:edit_mode] > 1
opts = {table: 'dc_page;dc_part', formname: 'dc_part', ids: @doc.id }
html << dc_link_for_create( opts.merge!({title: 'Add new part', 'dc_part.name' => 'initial name', 'dc_part.order' => 10}) )
end
Returns: String. HTML code which includes add image and javascript to invoke new document create action.
439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'app/helpers/dc_application_helper.rb', line 439 def dc_link_for_create(opts) opts.stringify_keys! title = opts.delete('title') # title = t(title, title) if title target = opts.delete('target') || 'iframe_cms' opts['form_name'] ||= opts['table'] opts['action'] = 'new' opts['controller'] ||= 'cmsedit' js = "$('##{target}').attr('src', '#{_origin.url_for(opts)}'); return false;" dc_link_to(nil, _origin.fa_icon('plus-circle lg', class: 'dc-inline-link'), '#', { onclick: js, title: title, alt: 'Create'}).html_safe end |
#dc_link_for_edit(opts) ⇒ Object
Will create HTML code required to edit document.
Parameters:
- opts
-
Hash. Optional parameters for url_for helper. These options must provide at least table and formname
and id parameters.
Example:
html << dc_link_for_edit( @options ) if @opts[:edit_mode] > 1
Returns: String. HTML code which includes edit image and javascript to invoke edit document action.
465 466 467 468 469 470 471 472 473 474 475 |
# File 'app/helpers/dc_application_helper.rb', line 465 def dc_link_for_edit(opts) opts.stringify_keys! title = opts.delete('title') # target = opts.delete('target') || 'iframe_cms' opts['controller'] ||= 'cmsedit' opts['action'] = 'edit' opts['form_name'] ||= opts['table'] js = "$('##{target}').attr('src', '#{_origin.url_for(opts)}'); return false;" dc_link_to(nil, _origin.fa_icon('edit lg', class: 'dc-inline-link'), '#', { onclick: js, title: title, alt: 'Edit'}) end |
#dc_link_for_edit1(opts, link_text) ⇒ Object
Create one option in page edit link. Subroutine of dc_page_edit_menu.
495 496 497 498 499 500 |
# File 'app/helpers/dc_application_helper.rb', line 495 def dc_link_for_edit1(opts, link_text) #:nodoc: icon = opts.delete('icon') url = _origin.url_for(opts) "<li><div class='drgcms_popmenu_item' style='cursor: pointer;' data-url='#{url}'> #{_origin.fa_icon(icon)} #{link_text}</div></li>\n" end |
#dc_link_menu_tag(title) {|html| ... } ⇒ Object
Create edit link with edit picture. Subroutine of dc_page_edit_menu.
480 481 482 483 484 485 486 487 488 489 490 |
# File 'app/helpers/dc_application_helper.rb', line 480 def (title) #:nodoc: html =<<EOT <dl> <dt><div class='drgcms_popmenu' href="#"> #{_origin.fa_icon('edit lg', class: 'dc-inline-link', title: title)}</div></dt> <dd> <ul class=' div-hidden drgcms_popmenu_class'> EOT yield html html << "</ul></dd></dl>" end |
#dc_link_to(caption, icon, parms, rest = {}) ⇒ Object
Similar to rails link_to, but also takes care of link icon, translation, …
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'app/helpers/dc_application_helper.rb', line 312 def dc_link_to(caption, icon, parms, rest={}) if parms.class == Hash parms.stringify_keys! rest.stringify_keys! rest['class'] = rest['class'].to_s + ' dc-animate' rest['target'] ||= parms.delete('target') end # if icon icon_image = if icon.match(/\./) _origin.image_tag(icon, class: 'dc-link-img dc-animate') elsif icon.match('<i') icon else _origin.fa_icon(icon) end end # if caption caption = t(caption, caption) icon_image << ' ' if icon_image end _origin.link_to("#{icon_image}#{caption}".html_safe, parms, rest) end |
#dc_name4_id(model, field, field_name, id = nil) ⇒ Object
Will return descriptive text for id key when field in one table (collection) has belongs_to relation to other table.
Parameters:
- model
-
String. Table (collection) model name (lowercase).
- field
-
String. Field name holding the value of descriptive text.
- field_name
-
String. ID field name. This is by default id, but can be any other
(preferred unique) field.
- value
-
Value of id_field. Usually a BSON Key but can be any other data type.
Example:
# usage in program.
dc_name4_id('dc_user', 'name', nil, dc_page.created_by)
# usage in form
columns:
2:
name: site_id
eval: dc_name4_id,site,name
# username is saved to document instead of user.id field
5:
name: user
eval: dc_name4_id,dc_user,name,username
Returns: String. Name (descriptive value) for specified key in table.
699 700 701 702 703 704 705 |
# File 'app/helpers/dc_application_helper.rb', line 699 def dc_name4_id(model, field, field_name, id=nil) return '' if id.nil? field_name = 'id' if field_name.nil? model = model.strip.classify.constantize if model.class == String rec = Mongoid::QueryCache.cache { model.find_by(field_name.strip.to_sym => id) } rec.nil? ? '' : rec[field] end |
#dc_name4_value(model, field, value) ⇒ Object
When select field is used on form options for select can be provided by helpers.label.table_name.choices4_name locale. This is how select field options are translated. Method returns selected choice translated to current locale.
Parameters:
- model
-
String. Table (collection) model name (lowercase).
- field
-
String. Field name used.
- value
-
String. Value of field which translation will be returned.
Example:
# usage in program. Choice values for state are 'Deactivated:0,Active:1,Waiting:2'
dc_name4_value('dc_user', 'state', @record.active )
# usage in form
columns:
2:
name: state
eval: dc_name4_value dc_user, state
Returns: String. Descriptive text (translated) for selected choice value.
644 645 646 647 648 649 650 |
# File 'app/helpers/dc_application_helper.rb', line 644 def dc_name4_value(model, field, value) return '' if value.nil? c = t('helpers.label.' + model + '.choices4_' + field ) a = c.chomp.split(',').inject([]) {|r,v| r << v.split(':') } a.each {|e| return e.first if e.last.to_s == value.to_s } '' end |
#dc_new_title ⇒ Object
Creates title for cmsedit new action dialog.
Returns: String. HTML code for title.
261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'app/helpers/dc_application_helper.rb', line 261 def dc_new_title() title = @form['form']['title'] # defined as form:title:new if title and title['new'] t( title['new'], title['new'] ) else if @form['table'] == 'dc_dummy' t( @form['title'], @form['title'] ) else "#{t('drgcms.new')} : #{t_tablename(@form['table'])}" end end end |
#dc_page_bottom ⇒ Object
Helper for adding additional css and javascript code added by documents and renderers during page rendering.
206 207 208 |
# File 'app/helpers/dc_application_helper.rb', line 206 def dc_page_bottom() %Q[<style type="text/css">#{@css}</style>#{javascript_tag @js}].html_safe end |
#dc_page_class ⇒ Object
Return page class model defined in site document page_class field.
Used in forms, when method must be called from page model and model is overwritten by user’s own model.
Example as used on form:
30:
name: link
type: text_with_select
eval: 'dc_page_class.all_pages_for_site(@parent.dc_get_site)'
564 565 566 |
# File 'app/helpers/dc_application_helper.rb', line 564 def dc_page_class() dc_get_site.page_class.classify.constantize end |
#dc_page_edit_menu(opts = @opts) ⇒ Object
Create edit menu for editing existing or creating new dc_page documents. Edit menu consists of for options.
-
Edit content. Will edit only body part od document.
-
Edit advanced. Will create edit form for editing all document fields.
-
New page. Will create new document and pass some initial data to it. Initial data is saved to cookie.
-
New part. Will create new part of document.
Parameters:
- opts
-
Hash. Optional parameters for url_for helper. These options must provide at least table and formname
and id parameters.
Example:
html << () if @opts[:edit_mode] > 1
Returns: String. HTML code required for manipulation of currently processed document.
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'app/helpers/dc_application_helper.rb', line 520 def (opts=@opts) return '' if opts[:edit_mode] < 2 # save some data to cookie. This can not go to session. page = opts[:page] || @page table = _origin.site.page_table kukis = { "#{table}.dc_design_id" => page.dc_design_id, "#{table}.menu_id" => page., "#{table}.kats" => page.kats, "#{table}.page_id" => page.id, "#{table}.dc_site_id" => _origin.site.id } _origin.[:record] = Marshal.dump(kukis) title = "#{t('drgcms.edit')}: #{page.subject}" (title) do |html| opts[:editparams].merge!( controller: 'cmsedit', action: 'edit', 'icon' => 'edit' ) opts[:editparams].merge!( :id => page.id, :table => _origin.site.page_table, formname: opts[:formname], edit_only: 'body' ) html << dc_link_for_edit1( opts[:editparams], t('drgcms.edit_content') ) # opts[:editparams][:edit_only] = nil opts[:editparams].merge!( edit_only: nil, 'icon' => 'pencil' ) html << dc_link_for_edit1( opts[:editparams], t('drgcms.edit_advanced') ) # opts[:editparams][:action] = 'new' opts[:editparams].merge!( action: 'new', 'icon' => 'plus' ) html << dc_link_for_edit1( opts[:editparams], t('drgcms.edit_new_page') ) opts[:editparams].merge!(ids: page.id, formname: 'dc_part', 'icon' => 'plus-square-o', table: "#{_origin.site.page_table};dc_part" ) html << dc_link_for_edit1( opts[:editparams], t('drgcms.edit_new_part') ) end end |
#dc_page_top ⇒ Object
Helper for rendering top CMS menu when in editing mode
194 195 196 197 198 199 200 |
# File 'app/helpers/dc_application_helper.rb', line 194 def dc_page_top() if @design and !@design.rails_view.blank? # Evaluate parameters in design body eval(@design.body) end session[:edit_mode] > 0 ? render(partial: 'cmsedit/edit_stuff') : '' end |
#dc_render(renderer, opts = {}) ⇒ Object
This is main method used for render parts of design into final HTML document.
Parameters:
- renderer
-
String or Symbol. Class name (in lowercase) that will be used to render final HTML code.
If class name is provided without ‘_renderer’ suffix it will be added automatically.
When renderer has value :part, it is a shortcut for dc_render_design_part method which is used to draw parts of layout on design.
- opts
-
Hash. Additional options that are passed to method. Options are merged with
options set on site, design, page and passed to renderer object.
Example:
<%= dc_render(:dc_page, method: 'view', category: 'news') %>
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/helpers/dc_application_helper.rb', line 87 def dc_render(renderer, opts={}) return dc_render_design_part(renderer[:part]) if renderer.class == Hash # opts[:edit_mode] = session[:edit_mode] opts[:editparams] = {} opts = @options.merge(opts) # merge options with parameters passed on site, page, design ... opts.symbolize_keys! # this makes lots of things easier # Create renderer object klass = renderer.to_s.downcase klass += '_renderer' unless klass.match('_renderer') # obj = Kernel.const_get(klass.classify, Class.new).new(self, opts) rescue nil # if obj html = obj.render_html @css << obj.render_css.to_s html.nil? ? '' : html.html_safe # nil can happened else "Class #{klass} not defined!" end end |
#dc_render_design(part) ⇒ Object
187 188 189 |
# File 'app/helpers/dc_application_helper.rb', line 187 def dc_render_design(part) dc_render_design_part(part) end |
#dc_render_design_part(part) ⇒ Object
Used for designs with lots of common code and one (or more) part which differs. Point is to define design once and replace some parts of design dinamically. Design may be defined in site and design doc defines only parts that vary from page to page.
Example: As used in design.
<%= dc_render_design_part(@main) %>
main variable is defined in design body for example:
@main = Proc.new {render partial: 'parts/home'}
This helper is replacement dc_render_from_site method which will soon be deprecated.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/helpers/dc_application_helper.rb', line 174 def dc_render_design_part(part) if part.nil? '' elsif part.class == Proc result = part.call result.class == Array ? result.first : result elsif part.class == String eval part else part.to_s end.html_safe end |
#dc_render_from_site(opts = {}) ⇒ Object
Used for designs with lots of common code and one (or more) part which differs. It will simply replace anchor code with value of variable.
Example: As used in design. Backslashing < and % is important <%
<% part = "<div class='some-class'>\<\%= dc_render(:my_renderer, method: 'render_method') \%\></div>" %>
<%= dc_replace_in_design(piece: 'piece_name', replace: '[main]', with: part) %>
Want to replace more than one part. Use array.
<%= dc_replace_in_design(replace: ['[part1]','[part2]'], with: [part1, part2]) %>
This helper is replacement for old ‘script’ method defined in dc_piece_renderer, but it uses design defined in site document if piece parameter is not set.
150 151 152 153 154 155 |
# File 'app/helpers/dc_application_helper.rb', line 150 def dc_render_from_site(opts={}) design = opts[:piece] ? DcPiece.find(name: opts[:piece]).script : dc_get_site.design layout = opts[:layout] || (dc_get_site.site_layout.size > 2 ? dc_get_site.site_layout : nil) render(inline: design, layout: opts[:layout], with: opts[:with]) end |
#dc_replace_in_design(opts = {}) ⇒ Object
Used for designs with lots of common code and one (or more) part which differs. It will simply replace anchor code with value of variable.
Example: As used in design. Backslashing < and % is important <%
<% part = "<div class='some-class'>\<\%= dc_render(:my_renderer, method: 'render_method') \%\></div>" %>
<%= dc_replace_in_design(piece: 'piece_name', replace: '[main]', with: part) %>
Want to replace more than one part. Use array.
<%= dc_replace_in_design(replace: ['[part1]','[part2]'], with: [part1, part2]) %>
This helper is replacement for old ‘script’ method defined in dc_piece_renderer, but it uses design defined in site document if piece parameter is not set.
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/helpers/dc_application_helper.rb', line 122 def dc_replace_in_design(opts={}) design = opts[:piece] ? DcPiece.find(name: opts[:piece]).script : dc_get_site.design layout = opts[:layout] || (dc_get_site.site_layout.size > 2 ? dc_get_site.site_layout : nil) if opts[:replace] # replace more than one part of code if opts[:replace].class == Array 0.upto(opts[:replace].size - 1) {|i| design.sub!(opts[:replace][i], opts[:with][i])} else design.sub!(opts[:replace], opts[:with]) end end render(inline: design, layout: nil) end |
#dc_submit_tag(caption, icon, parms, rest = {}) ⇒ Object
Similar to rails submit_tag, but also takes care of link icon, translation, …
295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'app/helpers/dc_application_helper.rb', line 295 def dc_submit_tag(caption, icon, parms, rest={}) parms['class'] ||= 'dc-submit' if icon icon_image = if icon.match(/\./) image_tag(icon, class: 'dc-animate') elsif icon.match('<i') icon else fa_icon(icon) end end html = icon_image || '' html << submit_tag(t(caption, caption), parms) end |
#dc_table_title(text, result_set = nil) ⇒ Object
String. HTML code for title.
220 221 222 223 224 225 226 227 |
# File 'app/helpers/dc_application_helper.rb', line 220 def dc_table_title(text, result_set=nil) c = "<table width='100%' class='dc-title'><tr><td>#{text}</td>" if result_set and result_set.respond_to?(:current_page) c << "<td align='right' style='font-size: 0.8em;'> #{paginate(result_set, :params => {:action => 'index'})}</td>" end c << '<tr></table>' c.html_safe end |
#dc_user_can_view(ctrl, policy_id) ⇒ Object
Will check if user roles allow user to view data in document with defined access_policy.
Parameters:
- ctrl
-
Controller object or object which holds methods to access session object. For example @parent
variable when called from renderer.
- policy_id
-
Document or documents policy_id field value required to view data. Method will automatically
check if parameter send has policy_id field defined and use value of that field.
Example:
can_view, = dc_user_can_view(@parent, @page)
# or
can_view, = dc_user_can_view(@parent, @page.policy_id)
return unless can_view
Returns: True if access_policy allows user to view data. False and message from policy that is blocking view if access is not allowed.
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
# File 'app/helpers/dc_application_helper.rb', line 980 def dc_user_can_view(ctrl, policy_id) policy_id = policy_id.policy_id if policy_id and policy_id.respond_to?(:policy_id) # Eventualy object without policy_id will be checked. This is to prevent error policy_id = nil unless policy_id.class == BSON::ObjectId # site = ctrl.site policies = site.dc_policies # permission defined by default policy default_policy = Mongoid::QueryCache.cache { policies.find_by(is_default: true) } return false, 'Default accsess policy not found for the site!' unless default_policy # = {} default_policy.dc_policy_rules.to_a.each { |v| [v.dc_policy_role_id] = v. } # update permissions with defined policy part_policy = nil if policy_id part_policy = Mongoid::QueryCache.cache { policies.find(policy_id) } return false, 'Access policy not found for part!' unless part_policy part_policy.dc_policy_rules.to_a.each { |v| [v.dc_policy_role_id] = v. } end # apply guest role if user has no roles defined if ctrl.session[:user_roles].nil? role = Mongoid::QueryCache.cache { DcPolicyRole.find_by(system_name: 'guest', active: true) } return false, 'System guest role not defined!' unless role ctrl.session[:user_roles] = [role.id] end # Check if user has any role that allows him to view part # p h, ctrl.session[:user_roles] can_view, msg = false,'' ctrl.session[:user_roles].each do |role| next unless [role] # role not yet defined. Will die in next line. if [role] > 0 can_view = true break end end msg = if !can_view part_policy ? t(part_policy.,part_policy.) : t(default_policy.,default_policy.) end return can_view, msg end |
#dc_user_has_role(role, user = nil, roles = nil) ⇒ Object
Check if user has required role assigned to its user profile. If role is passed as string method will check roles for name and system name.
Parameters:
- role
-
DcPolicyRole/String. Required role. If passed as string role will be searched in dc_policy_roles collection.
- user
-
User id. Defaults to session.
- roles
-
Array of roles that will be searched. Default session.
Example:
if dc_user_has_role('decision_maker', session[:user_id), session[:user_roles])
do_something_important
end
Returns: Boolean. True if user has required role.
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 |
# File 'app/helpers/dc_application_helper.rb', line 1039 def dc_user_has_role( role, user=nil, roles=nil ) roles = _origin.session[:user_roles] if roles.nil? user = _origin.session[:user_id] if user.nil? return false if user.nil? or roles.nil? # role = DcPolicyRole.get_role(role) return false if role.nil? # role is included in roles array roles.include?(role._id) end |
#decamelize_type(string) ⇒ Object
Decamelizes string. This probably doesn’t work very good with non ascii chars. Therefore it is very unwise to use non ascii chars for table (collection) names.
Parameters:
- string
-
String. String to be converted into decamelized string.
Returns: String. Decamelized string.
373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'app/helpers/dc_application_helper.rb', line 373 def decamelize_type(string) return nil if string.nil? r = '' string.to_s.each_char do |c| r << case when r.size == 0 then c.downcase when c.downcase != c then '_' + c.downcase else c end end r end |
#forms_merge(hash1, hash2) ⇒ Object
Merges two forms when current form extends other form. Subroutine of dc_choices4_cmsmenu. With a little help of www.ruby-forum.com/topic/142809
831 832 833 834 835 836 837 838 839 840 841 842 |
# File 'app/helpers/dc_application_helper.rb', line 831 def forms_merge(hash1, hash2) #:nodoc: target = hash1.dup hash2.keys.each do |key| if hash2[key].is_a? Hash and hash1[key].is_a? Hash target[key] = forms_merge(hash1[key], hash2[key]) next end target[key] = hash2[key] == '/' ? nil : hash2[key] end # delete keys with nil value target.delete_if{ |k,v| v.nil? } end |
#t(key, default = '') ⇒ Object
Wrapper for i18 t method, with some spice added. If translation is not found English translation value will be returned. And if still not found default value will be returned if passed.
Parameters:
- key
-
String. String to be translated into locale.
- default
-
String. Value returned if translation is not found.
Example:
t('translate.this','Enter text for ....')
Returns: String. Translated text.
582 583 584 585 586 587 588 589 590 591 592 |
# File 'app/helpers/dc_application_helper.rb', line 582 def t(key, default='') c = I18n.t(key) if c.class == Hash or c.match( 'translation missing' ) c = I18n.t(key, locale: 'en') # Still not found. Return default if set if c.class == Hash or c.match( 'translation missing' ) c = default.blank? ? key : default end end c end |
#t_name(field_name, default = '') ⇒ Object
Returns label for field translated to current locale for usage on data entry form. Translation is provided by lang.helpers.label.table_name.field_name locale. If translation is not found method will capitalize field_name and replace ‘_’ with ‘ ’.
614 615 616 617 618 |
# File 'app/helpers/dc_application_helper.rb', line 614 def t_name(field_name, default='') c = t("helpers.label.#{@form['table']}.#{field_name}", default) c = field_name.capitalize.gsub('_',' ') if c.match( 'translation missing' ) c end |
#t_tablename(tablename, default = nil) ⇒ Object
Returns table (collection) name translation for usage in dialog title. Tablename title is provided by helpers.label.table_name.tabletitle locale.
Parameters:
- tablename
-
String. Table (collection) name to be translated.
- default
-
String. Value returned if translation is not found.
Returns: String. Translated text.
605 606 607 |
# File 'app/helpers/dc_application_helper.rb', line 605 def t_tablename(tablename, default=nil) t('helpers.label.' + tablename + '.tabletitle', default || tablename) end |