Module: Frontline::Helpers
- Includes:
- Inflector
- Defined in:
- lib/frontline/helpers.rb
Constant Summary collapse
- @@association_hints =
{}
Instance Method Summary collapse
-
#applications ⇒ Object
return the list of managed applications.
- #association_hint_class(assoc) ⇒ Object
- #association_hints ⇒ Object
- #clear_registry_cache! ⇒ Object
-
#controllers ⇒ Object
return the list of controllers for effective application.
-
#datamapper? ⇒ Boolean
determine whether effective application is using DataMapper ORM.
-
#enginery_registry(unit) ⇒ Object
execute a enginery registry command.
- #maintenance_files ⇒ Object
-
#models ⇒ Object
return the list of models for effective application.
- #pluralized_association_hints ⇒ Object
- #postcrud_handlers ⇒ Object
-
#pty_spawn(cmd, opts = {}) ⇒ Object
running given command via ‘PTY.spawn` and streaming each line to browser.
-
#pty_stream(event, data = '') ⇒ Object
send data to browser via EventSource socket.
- #singularized_association_hints ⇒ Object
- #stringify_application(name, path, url) ⇒ Object
Methods included from Inflector
#inflections, #pluralize, #singularize, #underscore
Instance Method Details
#applications ⇒ Object
return the list of managed applications.
20 21 22 |
# File 'lib/frontline/helpers.rb', line 20 def applications (a = ['Applications']) ? JSON.load(a) : [] end |
#association_hint_class(assoc) ⇒ Object
139 140 141 142 143 |
# 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 |
#association_hints ⇒ Object
127 128 129 |
# File 'lib/frontline/helpers.rb', line 127 def association_hints @@association_hints[models.__id__] ||= models.keys.map {|x| underscore(x.to_s)} end |
#clear_registry_cache! ⇒ Object
145 146 147 148 |
# File 'lib/frontline/helpers.rb', line 145 def clear_registry_cache! CONTROLLERS.clear MODELS.clear end |
#controllers ⇒ Object
return the list of controllers for effective application
25 26 27 28 29 30 |
# 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 |
#datamapper? ⇒ Boolean
determine whether effective application is using DataMapper ORM
122 123 124 |
# 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 |
#enginery_registry(unit) ⇒ Object
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.
45 46 47 48 49 50 51 52 53 54 55 |
# 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 |
#maintenance_files ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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 |
#models ⇒ Object
return the list of models for effective application
33 34 35 36 37 38 |
# 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 |
#pluralized_association_hints ⇒ Object
135 136 137 |
# File 'lib/frontline/helpers.rb', line 135 def pluralized_association_hints association_hints.map {|m| pluralize(m)} end |
#postcrud_handlers ⇒ Object
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 |
# 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.] + 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 |
#pty_spawn(cmd, opts = {}) ⇒ Object
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.
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 |
# 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.) e.backtrace.each {|l| pty_stream('content', div_tag(l))} [false, failure_id] end |
#pty_stream(event, data = '') ⇒ Object
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.
13 14 15 16 17 |
# File 'lib/frontline/helpers.rb', line 13 def pty_stream event, data = '' return unless stream = STREAMS[@uuid] stream.event event stream.data data end |
#singularized_association_hints ⇒ Object
131 132 133 |
# File 'lib/frontline/helpers.rb', line 131 def singularized_association_hints association_hints.map {|m| singularize(m)} end |
#stringify_application(name, path, url) ⇒ Object
57 58 59 |
# File 'lib/frontline/helpers.rb', line 57 def stringify_application name, path, url JSON.dump([name, path, url]) end |