class Voom::Presenters::WebClient::App

Public Instance Methods

_build_script_tag_(path) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 135
          def _build_script_tag_(path)
            (<<~JS)
            <script defer src="#{env['SCRIPT_NAME']}#{path.sub('public/','')}"></script>
            JS
          end
color_classname(comp, affects = nil, color_attr = :color) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 79
def color_classname(comp, affects = nil, color_attr = :color)
  color = comp&.public_send(color_attr)
  return unless color

  return "v-#{comp.type}__primary" if eq(color, :primary)
  return "v-#{comp.type}__secondary" if eq(color, :secondary)

  "v-#{affects}color__#{color}"
end
color_style(comp, affects = nil, color_attr = :color) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 89
def color_style(comp, affects = nil, color_attr = :color)
  color = comp.public_send(color_attr)
  "#{affects}color: #{color};" unless %w(primary secondary).include?(color.to_s) || color.nil?
end
custom_css(path, host=nil) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 124
def custom_css(path, host=nil)
  CustomCss.new(path, root: Presenters::Settings.config.presenters.root, host: host).render
end
custom_js() click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 128
def custom_js
  custom_js_path = Presenters::Settings.config.presenters.web_client.custom_js
  Dir.glob(custom_js_path).map do |file|
    _build_script_tag_(file)
  end.join("\n") if custom_js_path
end
eq(attrib, value) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 51
def eq(attrib, value)
  attrib.to_s == value.to_s
end
expand_text(text, markdown: true) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 71
def expand_text(text, markdown: true)
  if markdown
    self.markdown(Array(text).join("\n\n")) #.gsub("\n\n", "<br/>")
  else
    Array(text).join('<br/>')
  end
end
h(text) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 55
def h(text)
  Rack::Utils.escape_html(text)
end
include?(array, value) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 59
def include?(array, value)
  array.map(&:to_s).include?(value.to_s)
end
includes_one?(array1, array2) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 63
def includes_one?(array1, array2)
  (array2.map(&:to_sym)-array1.map(&:to_sym)).size != array2.size
end
inflector() click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 47
def inflector
  @inflector ||= Dry::Inflector.new
end
markdown(text) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 29
def markdown(text)
  unless @markdown
    renderer = CustomRender.new(hard_wrap: false, filter_html: true)
    options = {
        autolink: false,
        no_intra_emphasis: true,
        fenced_code_blocks: true,
        lax_html_blocks: true,
        strikethrough: true,
        superscript: true,
        disable_indented_code_blocks: true
    }
    @markdown = Redcarpet::Markdown.new(renderer, options)
  end

  @markdown.render(text)
end
plugin_headers(pom) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 120
def plugin_headers(pom)
  PluginHeaders.new(pom: pom, render: method(:render)).render
end
render_component(scope, comp, components, index) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 25
def render_component(scope, comp, components, index)
  ComponentRenderer.new(comp, render: method(:render), scope: scope, components: components, index: index).render
end
snake_to_camel(hash, except: []) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 94
def snake_to_camel(hash, except: [])
  Hash[hash.map {|k, v|
    next [k, v] if except.include?(k)
    new_key = k.to_s.split('_').collect(&:capitalize).join
    new_key[0] = new_key[0].downcase
    [new_key, v]}
  ]
end
to_hash(ostruct_or_hash) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 103
def to_hash(ostruct_or_hash)
  {}.tap do |h|
    ostruct_or_hash.to_h.each {|key, value| h[key.to_sym] = transform(value)}
  end
end
transform(thing) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 109
def transform(thing)
  case thing
  when OpenStruct
    to_hash(thing)
  when Array
    thing.map {|v| transform(v)}
  else
    thing
  end
end
unique_id(comp) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 67
def unique_id(comp)
  "#{comp.id}-#{SecureRandom.hex(4)}"
end

Private Instance Methods

prepare_context(base_params = params) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 222
def prepare_context(base_params = params)
  prepare_context = Presenters::Settings.config.presenters.web_client.prepare_context.dup
  prepare_context.push(method(:scrub_context))
  context = base_params.dup
  prepare_context.reduce(context) do |params, context_proc|
    context = context_proc.call(params, session, env)
  end
  context
end
render_presenter(presenter) click to toggle source

analogous to Voom::Presenters::Api::App#render_presenter

# File lib/voom/presenters/web_client/app.rb, line 183
def render_presenter(presenter)
  @grid_nesting = Integer(params[:grid_nesting] || 0)

  begin
    before_render = Presenters::Settings.config.presenters.before_render
    render_instead, ctx = before_render
                            .lazy
                            .map { |p| p.call(request) }
                            .detect(&:itself)

    if Presenters::App.registered?(render_instead)
      presenter = Presenters::App[render_instead].call
    end

    p = params.merge(ctx || {})
    @pom = presenter.expand(router: router, context: prepare_context(p))
    @base_url = request.base_url
    layout = !(request.env['HTTP_X_NO_LAYOUT'] == 'true')
    response.headers['X-Frame-Options'] = ENV['ALLOWALL_FRAME_OPTIONS'] || presenter.options.fetch(:allow_all_frame_options, false) ? 'ALLOWALL' : 'SAMEORIGIN'
    erb :web, layout: layout
  rescue StandardError => e
    Presenters::Settings.config.presenters.error_logger.call(
      @env['rack.errors'],
      e,
      params,
      presenter.name
    )
    raise e
  rescue Presenters::Errors::Unprocessable => e
    content_type :json
    status 422
    JSON.dump({error: e.message})
  end
end
router() click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 218
def router
  settings.router_.new(base_url: "#{request.base_url}")
end
scrub_context(params, _session, _env) click to toggle source
# File lib/voom/presenters/web_client/app.rb, line 232
def scrub_context(params, _session, _env)
  %i(splat captures  grid_nesting input_tag).each do |key|
    params.delete(key) {params.delete(key.to_s)}
  end
  params
end