module Frontline::Helpers
Public Instance Methods
return the list of managed applications.
# File lib/frontline/helpers.rb, line 20 def applications (a = cookies['Applications']) ? JSON.load(a) : [] end
# File lib/frontline/helpers.rb, line 139 def association_hint_class assoc assoc == :belongs_to || assoc == :has_one ? 'singularized_association_hints' : 'pluralized_association_hints' end
# File lib/frontline/helpers.rb, line 127 def association_hints @@association_hints[models.__id__] ||= models.keys.map {|x| underscore(x.to_s)} end
# File lib/frontline/helpers.rb, line 145 def clear_registry_cache! CONTROLLERS.clear MODELS.clear end
return the list of controllers for effective application
# File lib/frontline/helpers.rb, line 25 def controllers return CONTROLLERS[dst_path.root] if CONTROLLERS[dst_path.root] result = enginery_registry(:c) return CONTROLLERS[dst_path.root] = result if result.is_a?(Hash) result end
determine whether effective application is using DataMapper ORM
# File lib/frontline/helpers.rb, line 122 def datamapper? (m = models) && m.is_a?(Hash) && (m = models.values.first) && m[:orm].to_s =~ /\Ad/i end
execute a enginery registry command. data are extracted via `$ enginery -c` command so it comes in YAML format. if call was successful, parse the YAML and return the result as a Hash. otherwise return the stdout+stderr as a String. if YAML parser failing, a exception will be raised.
# File lib/frontline/helpers.rb, line 45 def enginery_registry unit Dir.chdir dst_path.root do cmd = '%s -%s' % [Enginery::EXECUTABLE, unit] stdout, stderr, status = Open3.capture3(cmd) if status && status.exitstatus == 0 YAML.load(stdout) else [stdout, stderr].join("\n") end end end
# File lib/frontline/helpers.rb, line 106 def maintenance_files files = %w[ Gemfile Rakefile base/boot.rb config/config.yml config/database.yml base/helpers/application_helpers.rb ] Dir[dst_path(:views, 'layout*')].each do |e| File.file?(e) && files.unshift('base/views/' + File.basename(e)) end files end
return the list of models for effective application
# File lib/frontline/helpers.rb, line 33 def models return MODELS[dst_path.root] if MODELS[dst_path.root] result = enginery_registry(:m) return MODELS[dst_path.root] = result if result.is_a?(Hash) result end
# File lib/frontline/helpers.rb, line 135 def pluralized_association_hints association_hints.map {|m| pluralize(m)} end
# File lib/frontline/helpers.rb, line 150 def postcrud_handlers cache = if action_name[0] == 'm' MODELS.delete(dst_path.root) models() else CONTROLLERS.delete(dst_path.root) controllers() end errors = [] if cache.is_a?(Hash) begin pty_stream 'render', render_p(action_name, action_params) pty_stream 'modal', 'hide' rescue => e errors = [e.message] + e.backtrace end else errors = cache.to_s.split("\n") end if errors.any? html = hr_tag << div_tag(class: 'alert alert-error lead') { 'Error loading %ss' % action_name } << errors.map {|e| div_tag(e, class: 'text-error')}.join pty_stream('content', html) end end
running given command via `PTY.spawn` and streaming each line to browser. actions calling this helper should firstly set `@uuid` variable for streaming to work.
before executing given cmd it will change working directory to effective application root(see `Index#get_application`).
if some exception raised it will be rescued and error message alongside backtrace will be streamed to browser.
it returns an Array first element of which is the status and the second is a unique ID used to identify lines generated by given cmd. if status is negative, actions calling this helper will can identify lines and mark them as errored.
# File lib/frontline/helpers.rb, line 75 def pty_spawn cmd, opts = {} failure_id = 'pty_' << cmd.__id__.to_s root = opts[:root] || dst_path.root pty_stream 'content', div_tag!('$ cd ' << root, class: 'muted') pty_stream 'content', div_tag!(b_tag('$ ' << cmd, class: 'text-info')) Dir.chdir root do PTY.spawn cmd do |r, w, pid| begin r.sync r.each_line do |line| line.rstrip! # div_tag will escape line before emitting it html = line.empty? ? br_tag : div_tag(line, class: failure_id) pty_stream 'content', html end rescue Errno::EIO # simply ignoring this ensure ::Process.wait pid end end end [$? && $?.exitstatus == 0, failure_id] rescue => e pty_stream 'content', p_tag(e.message) e.backtrace.each {|l| pty_stream('content', div_tag(l))} [false, failure_id] end
send data to browser via EventSource socket. socket should be earlier set via `Index#get_streamer`. actions calling this helper should firstly set `@uuid` variable.
@param [String] event
event to be sent to browser
@param [String] data
data to be sent to browser
# File lib/frontline/helpers.rb, line 13 def pty_stream event, data = '' return unless stream = STREAMS[@uuid] stream.event event stream.data data end
# File lib/frontline/helpers.rb, line 131 def singularized_association_hints association_hints.map {|m| singularize(m)} end
# File lib/frontline/helpers.rb, line 57 def stringify_application name, path, url JSON.dump([name, path, url]) end