Module: Optimacms::ApplicationHelper

Included in:
ApplicationController
Defined in:
app/helpers/optimacms/application_helper.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



8
9
10
11
12
# File 'app/helpers/optimacms/application_helper.rb', line 8

def method_missing(method, *args, &block)
  main_app.send(method, *args, &block)
rescue NoMethodError
  super
end

Instance Method Details

#block(name, opts = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
# File 'app/helpers/optimacms/application_helper.rb', line 125

def block(name, opts={})
  x = Dir.pwd
  #y = File.expand_path File.dirname(__FILE__)

  d = File.dirname(@optimacms_tpl)

  # extensions

  extensions = ['', '.html.haml', '.html.erb', '.html']


  # try. HTML file in the current folder

  f = File.join(Dir.pwd, 'app', 'views', d, name+'.html')
  #return render file: f if File.exists? f


  if !File.exists?(f)

    #

    names = []

    #

    parts = name.split /\//

    parts[-1] = '_'+parts[-1]
    name2 = parts.join('/')

    #

    names << [d, name]
    names << ["", name]

    names << [d, name2]
    names << ["", name2]


    # try 2

    names.each do |p|
      extensions.each do |ext|
        # with lang

        ff = File.join(Dir.pwd, 'app', 'views', p[0], p[1]+'.'+@pagedata.lang.to_s+ext)
        #(return render(file: f))      if File.exists? f

        if File.exists? ff
          f = ff
          break
        end

        # without lang

        ff = File.join(Dir.pwd, 'app', 'views', p[0], p[1]+ext)
        #(return render(file: f))      if File.exists? f

        if File.exists? ff
          f = ff
        end
      end

      #

      break if File.exists? f
    end
  end

  # render from file

  if File.exists? f
    #opts[:file] = f

    #return render opts


    if current_cms_admin_user
      #return block_with_edit file: f, locals: opts

      return block_with_edit name, f, opts
    else
      return render file: f, locals: opts
    end

  end


  # default render

  render name, opts
end

#block_with_edit(name, tpl_filename, opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/optimacms/application_helper.rb', line 66

def block_with_edit(name, tpl_filename, opts={})

  # find template

  row_tpl = Optimacms::PageServices::TemplateService.get_by_name(name)

  # data relations

  tpl_data_relations = {}
  if row_tpl
    tpl_data_relations = row_tpl.data_relations.all.index_by { |t| t.var_name }
  end


  # map locals and edit links

  links_data = []

  opts.each do |_var_name, v|
    var_name = _var_name.to_s

    next unless tpl_data_relations[var_name]
    data = tpl_data_relations[var_name]

    #if class exists $modelDecorator

    #klass = Object.const_get "#{p['model']}CmsDecorator"

    #objDecorator = klass.new(v)

    #classify.

    modelEditDecorator = "#{data.data_model_name}CmsDecorator".safe_constantize.new(v)

    next unless modelEditDecorator

    link_edit = modelEditDecorator.edit_path

    next unless link_edit

    #p = tpl_data_relations[var_name]

    #id = v.send(:id)

    #links_edit << {link: send(p["root_edit"]+"_path", id)}

    link_html = link_to("edit #{data.title}", link_edit, target: "_blank")
    links_data << {link: link_edit, link_html: link_html}
  end



  # render

  return render template: 'optimacms/admin_page_edit/block_edit', locals: {filename: tpl_filename, row_tpl: row_tpl}

  (:div, class: "debug_box") do
    #(link_to "edit", "/admin/templates/#{row_tpl.id}/edit", target: "_blank")+

    (:div, class: "debug_commands") do
      html_links = links_data.map{|r| r[:link_html]}.join("<br>").html_safe

      ((link_to "edit block", "/admin/templates/#{row_tpl.id}/edit", target: "_blank")+"<br>".html_safe+html_links).html_safe
    end+
    #block(name, opts)

    (render file: tpl_filename, locals: opts)
  end
end

#eval_meta_string(s) ⇒ Object



18
19
20
# File 'app/helpers/optimacms/application_helper.rb', line 18

def eval_meta_string(s)
  eval('"'+s+'"')
end

#fix_quotes(s) ⇒ Object



22
23
24
# File 'app/helpers/optimacms/application_helper.rb', line 22

def fix_quotes(s)
  s.gsub /"/, '\"'
end

#meta_tags(title = nil, keywords = nil, desc = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/optimacms/application_helper.rb', line 48

def meta_tags(title=nil, keywords=nil, desc=nil)
  #s = 'news "11"'

  #s1 = s.gsub /"/, '\"'

  #s2 = eval('"'+s1+'"')

  title ||= eval_meta_string(fix_quotes(@optimacms_meta_title))
  keywords ||= eval_meta_string(@optimacms_meta_keywords)
  desc ||= eval_meta_string(@optimacms_meta_description)


return %(<title>#{title}</title>
<meta name="keywords" content="#{keywords}"/>
<meta name="description" content="#{desc}"/>
  ).html_safe
  #content_tag(:title, title)+

  #content_tag(:meta, nil, content: keywords, name: 'keywords')+

  #content_tag(:meta, nil, content: desc, name: 'description')

end

#msgObject



201
202
203
# File 'app/helpers/optimacms/application_helper.rb', line 201

def msg
  Optimacms::Resource
end

#tinymce_editor_insert_blockObject



14
15
16
# File 'app/helpers/optimacms/application_helper.rb', line 14

def tinymce_editor_insert_block
  '<pre>{{block:name:sub}}</pre>'
end

#url_for(options = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/optimacms/application_helper.rb', line 26

def url_for(options = nil)
  #url_for

  # page processed by CMS

  if options.present? && options.kind_of?(Hash) && options[:controller]=='optimacms/pages' && options[:action]=='show'

    # get current page name

    page_name = options[:page_name] || controller.optimacms_pagedata.page.name
    if page_name.present?
      u = site_page_path(page_name, options)

      if u.present?
        return u
      end
    end
  end

  #

  return super(options)
end