Module: RbbtRESTHelpers
- Defined in:
- lib/rbbt/rest/common/misc.rb,
lib/rbbt/rest/common/cache.rb,
lib/rbbt/rest/common/forms.rb,
lib/rbbt/rest/common/table.rb,
lib/rbbt/rest/common/locate.rb,
lib/rbbt/rest/common/render.rb,
lib/rbbt/rest/common/resources.rb
Defined Under Namespace
Classes: Retry, TemplateMissing
Constant Summary collapse
- PAGE_SIZE =
20
- MEMORY_CACHE =
{}
Instance Attribute Summary collapse
-
#javascript_resources ⇒ Object
Returns the value of attribute javascript_resources.
-
#plugin_resources ⇒ Object
Returns the value of attribute plugin_resources.
-
#sass_resources ⇒ Object
Returns the value of attribute sass_resources.
-
#template_resources ⇒ Object
Returns the value of attribute template_resources.
Class Method Summary collapse
-
.add_sass_load_path(path) ⇒ Object
{{{ SASS.
-
.javascript_resources ⇒ Object
{{{ JAVASCRIPT.
- .load_tsv(file) ⇒ Object
- .sass_resources ⇒ Object
- .save_tsv(tsv, path) ⇒ Object
-
.template_resources ⇒ Object
{{{ TEMPLATE.
Instance Method Summary collapse
- #add_GET_param(url, param, value) ⇒ Object
- #cache(name, params = {}, &block) ⇒ Object
- #consume_parameter(parameter, params = nil) ⇒ Object
- #development? ⇒ Boolean
- #error_for(job, layout = nil) ⇒ Object
- #file_or_text_area(id, name, value) ⇒ Object
- #filter(field, type = :string) ⇒ Object
- #fix_input(type, value, param_file = nil) ⇒ Object
- #form_input(name, type, default = nil, current = nil, description = nil, id = nil, extra = {}) ⇒ Object
- #fragment(link = nil, &block) ⇒ Object
- #hash2dl(hash, options = {}) ⇒ Object
- #header(field, entity_type, entity_options = {}) ⇒ Object
- #html_tag(*args) ⇒ Object
- #input_label(id, name, description = nil, default = nil, options = {}) ⇒ Object
- #link_css(file) ⇒ Object
- #link_js(file) ⇒ Object
- #load_tsv(file) ⇒ Object
- #locate_javascript(script) ⇒ Object
- #locate_javascript_from_resource(resource, script) ⇒ Object
- #locate_sass(template) ⇒ Object
- #locate_sass_from_resource(resource, template) ⇒ Object
- #locate_template(template) ⇒ Object
- #locate_template_from_resource(resource, template) ⇒ Object
- #log(status, message) ⇒ Object
- #modal_fragment(text, &block) ⇒ Object
- #old_cache(path, check) ⇒ Object
- #paginate(object, page = nil, just_keys = false) ⇒ Object
- #param2boolean(value) ⇒ Object
- #parse_page(page) ⇒ Object
- #partial_render(template, locals = {}, cache = nil, cache_options = {}) ⇒ Object
- #permalink(path) ⇒ Object
- #process_common_parameters ⇒ Object
- #production? ⇒ Boolean
- #record_css(file) ⇒ Object
- #record_js(file) ⇒ Object
- #recorded_css_files ⇒ Object
- #recorded_js_files ⇒ Object
- #remove_GET_param(url, param) ⇒ Object
- #render(template_file, locals = {}, layout_file = nil, cache = nil, cache_options = {}) ⇒ Object
- #render_partial(template_file, locals = {}, cache = nil, cache_options = {}) ⇒ Object
- #resource(filename = nil, text = nil, type = nil, options = {}) {|f| ... } ⇒ Object
- #reveal(text, id = nil, &block) ⇒ Object
- #save_tsv(file) ⇒ Object
- #serve_css ⇒ Object
- #serve_js(compress = true) ⇒ Object
- #table(options = {}) ⇒ Object
- #table_value(value, type = nil, options = {}) ⇒ Object
- #template_render(template, locals = {}, cache = nil, cache_options = {}) ⇒ Object
- #tsv2html(file, default_table_options = {}) ⇒ Object
- #tsv_process(tsv, filter = nil, column = nil) ⇒ Object
- #tsv_rows(tsv, page = nil, filter = nil, column = nil) ⇒ Object
- #tsv_rows_full(tsv) ⇒ Object
- #wait_on(job, layout = nil) ⇒ Object
Instance Attribute Details
#javascript_resources ⇒ Object
Returns the value of attribute javascript_resources.
2 3 4 |
# File 'lib/rbbt/rest/common/locate.rb', line 2 def javascript_resources @javascript_resources end |
#plugin_resources ⇒ Object
Returns the value of attribute plugin_resources.
2 3 4 |
# File 'lib/rbbt/rest/common/locate.rb', line 2 def plugin_resources @plugin_resources end |
#sass_resources ⇒ Object
Returns the value of attribute sass_resources.
2 3 4 |
# File 'lib/rbbt/rest/common/locate.rb', line 2 def sass_resources @sass_resources end |
#template_resources ⇒ Object
Returns the value of attribute template_resources.
2 3 4 |
# File 'lib/rbbt/rest/common/locate.rb', line 2 def template_resources @template_resources end |
Class Method Details
.add_sass_load_path(path) ⇒ Object
{{{ SASS
31 32 33 |
# File 'lib/rbbt/rest/common/locate.rb', line 31 def self.add_sass_load_path(path) Sass::Engine::DEFAULT_OPTIONS[:load_paths].unshift path end |
.javascript_resources ⇒ Object
{{{ JAVASCRIPT
58 59 60 |
# File 'lib/rbbt/rest/common/locate.rb', line 58 def self.javascript_resources @javascript_resources ||= [Rbbt.share.views.public.find(:lib), Rbbt.share.views.public.js.find(:lib)] end |
.load_tsv(file) ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rbbt/rest/common/table.rb', line 240 def self.load_tsv(file) tsv = TSV.open(Open.open(file)) = File.exists?(file + '.table_options') ? YAML.load_file(file + '.table_options') : {} tsv. = [:tsv_entity_options] headers = [:headers] headers.each{|field,p| tsv.entity_templates[field] = Misc.prepare_entity("TEMPLATE", p.first, (tsv. || {}).merge(p.last)) } unless headers.nil? [tsv, ] end |
.sass_resources ⇒ Object
35 36 37 |
# File 'lib/rbbt/rest/common/locate.rb', line 35 def self.sass_resources @sass_resources ||= [Rbbt.share.views.compass.find(:lib)] end |
.save_tsv(tsv, path) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rbbt/rest/common/table.rb', line 223 def self.save_tsv(tsv, path) Open.write(path, tsv.to_s) = {:tsv_entity_options => tsv.} if tsv.entity_templates and tsv.entity_templates.any? [:headers] ||= {} tsv.entity_templates.each do |field,template| next if [:headers].include? field info = template.info info.delete :format info.delete :annotation_types info.delete :annotated_array [:headers][field] = [template.annotation_types.last.to_s, info] end end Open.write(path + '.table_options', .to_yaml ) end |
.template_resources ⇒ Object
{{{ TEMPLATE
8 9 10 |
# File 'lib/rbbt/rest/common/locate.rb', line 8 def self.template_resources @template_resources ||= [Rbbt.share.views.find(:lib)] end |
Instance Method Details
#add_GET_param(url, param, value) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/rbbt/rest/common/misc.rb', line 175 def add_GET_param(url, param, value) if url =~ /\?.+=/ url + "&#{ param }=#{ value }" else url + "?#{ param }=#{ value }" end end |
#cache(name, params = {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 122 123 124 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 |
# File 'lib/rbbt/rest/common/cache.rb', line 16 def cache(name, params = {}, &block) @cache_type ||= params[:cache_type] if params[:cache_type] return yield if name.nil? or cache_type.nil? or cache_type == :none send_file = consume_parameter(:_send_file, params) # Setup Step check = [params[:_template_file]].compact check += consume_parameter(:_cache_check, params) || [] check.flatten! name += "_" << Misc.hash2md5(params) if params.any? path = File.join(settings.cache_dir, name) task = Task.setup(:name => "Sinatra cache", :result_type => :string, &block) step = Step.new(path, task, nil, nil, self) last_modified File.mtime(step.path.find) if step.done? halt 200, step.info.to_json if @format == :info self.instance_variable_set("@step", step) # Return fragment if @fragment fragment_file = step.file(@fragment) if File.exists?(fragment_file) case @format.to_s when "table" halt 200, tsv2html(fragment_file) when "json" halt 200, tsv_process(load_tsv(fragment_file).first).to_json when "tsv" content_type "text/tab-separated-values" halt 200, tsv_process(load_tsv(fragment_file).first).to_s when "values" tsv = tsv_process(load_tsv(fragment_file).first) list = tsv.values.flatten content_type "application/json" halt 200, list.compact.to_json when "entities" tsv = tsv_process(load_tsv(fragment_file).first) list = tsv.values.flatten tsv.prepare_entity(list, tsv.fields.first, tsv.) type = list.annotation_types.last list_id = "List of #{type} in table #{ @fragment }" list_id << " (#{ @filter })" if @filter Entity::List.save_list(type.to_s, list_id, list, user) header "Location", Entity::REST.entity_list_url(list_id, type) redirect to(Entity::REST.entity_list_url(list_id, type)) when "map" tsv = tsv_process(load_tsv(fragment_file).first) type = tsv.keys.annotation_types.last column = tsv.fields.first map_id = "Map #{type}-#{column} in #{ @fragment }" map_id << " (#{ @filter.gsub(';','|') })" if @filter Entity::Map.save_map(type.to_s, column, map_id, tsv, user) url = Entity::REST.entity_map_url(map_id, type, column) redirect to(url) when "excel" require 'rbbt/tsv/excel' tsv = load_tsv(fragment_file).first content_type "text/html" data = nil excel_file = TmpFile.tmp_file tsv.excel(excel_file, :name => @excel_use_name,:sort_by => @excel_sort_by, :sort_by_cast => @excel_sort_by_cast) send_file excel_file, :type => 'application/vnd.ms-excel', :filename => 'table.xls' else send_file fragment_file end else if File.exists?(fragment_file + '.error') halt 500, html_tag(:span, File.read(fragment_file + '.error'), :class => "message") + html_tag(:ul, File.read(fragment_file + '.backtrace').split("\n").collect{|l| html_tag(:li, l)} * "\n", :class => "backtrace") else halt 202, "Fragment not completed" end end end # Clean/update job if old_cache(step.path, check) or update == :reload begin pid = step.info[:pid] step.abort if pid and Misc.pid_exists? pid step.pid = nil rescue Exception Log.medium $!. end step.clean end # Issue step.fork unless step.started? step.join if cache_type == :synchronous or cache_type == :sync if update == :reload url = request.url url = remove_GET_param(url, :_update) url = remove_GET_param(url, :_) redirect to(url) end # Monitor begin if step.done? case when @permalink redirect to(permalink(step.path)) when send_file send_file step.path else step.load end else case step.status when :error, :aborted error_for step, !@ajax else # check for problems if File.exists?(step.info_file) and Time.now - File.atime(step.info_file) > 60 Log.debug("Checking on #{step.info_file}") running = step.running? if FalseClass === running Log.debug("Aborting zombie #{step.info_file}") step.abort unless step.done? raise RbbtRESTHelpers::Retry end FileUtils.touch(step.info_file) end wait_on step, false end end rescue RbbtRESTHelpers::Retry retry end end |
#consume_parameter(parameter, params = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rbbt/rest/common/misc.rb', line 88 def consume_parameter(parameter, params = nil) params = self.params if params.nil? val = params.delete(parameter.to_sym) val = params.delete(parameter.to_s) if val.nil? val = nil if String === val and val.empty? val end |
#development? ⇒ Boolean
16 17 18 |
# File 'lib/rbbt/rest/common/misc.rb', line 16 def development? ENV["RACK_ENV"] == "develoment" end |
#error_for(job, layout = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rbbt/rest/common/render.rb', line 5 def error_for(job, layout = nil) if @format == :json halt 400, {"message" => job.[-2], "backtrace" => job.info[:backtrace]}.to_json else layout = @layout if layout.nil? layout_file = (layout ? locate_template('layout') : nil) template_file = locate_template('error') result = render template_file, {:job => job}, layout_file halt 400, result end end |
#file_or_text_area(id, name, value) ⇒ Object
18 19 20 21 22 |
# File 'lib/rbbt/rest/common/forms.rb', line 18 def file_or_text_area(id, name, value) html_tag("input", nil, :type => "file", :id => (id.nil? ? nil : id + "__" + "param_file"), :name => name.to_s + "__" + "param_file") + html_tag("span", "or use the text area bellow", :class => "file_or_text_area") + html_tag("textarea", value || "" , :name => name, :id => id ) end |
#filter(field, type = :string) ⇒ Object
218 219 220 221 |
# File 'lib/rbbt/rest/common/table.rb', line 218 def filter(field, type = :string) @table_filters ||= {} @table_filters[field] = type end |
#fix_input(type, value, param_file = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 |
# File 'lib/rbbt/rest/common/misc.rb', line 99 def fix_input(type, value, param_file = nil) case type when nil, :string, :select value when :integer value.to_i when :float value.to_f when :multiple value.keys when :boolean param2boolean(value) when :text if param_file and (value.nil? or (String === value and value.empty?)) param_file[:tempfile].read else value.gsub(/\r\n/, "\n") end when :array text = if param_file and (value.nil? or (String === value and value.empty?)) param_file[:tempfile].read else value end case when Array === text text when text =~ /^list:([^:]+):(.+)$/ Entity::List.load_list($1, $2, user) when text =~ /\[.*\]/ JSON.parse(text) when text =~ /\n/ text.split(/\r?\n/).collect{|l| l.strip} when text =~ /\|/ text.split(/\|/).collect{|l| l.strip} else text.split(/,/).collect{|l| l.strip} end when :tsv if param_file and (value.nil? or (String === value and value.empty?)) TSV.open(param_file[:tempfile].open) else TSV.open(StringIO.new(value)) end end end |
#form_input(name, type, default = nil, current = nil, description = nil, id = nil, extra = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 122 123 124 125 126 127 128 129 130 |
# File 'lib/rbbt/rest/common/forms.rb', line 24 def form_input(name, type, default = nil, current = nil, description = nil, id = nil, extra = {}) = consume_parameter(:html_options, extra) || {} no_file = extra[:no_file] if extra case type when :multiple choices = consume_parameter(:choices, extra) default = default.collect{|o| o.to_s} if default current = current.collect{|o| o.to_s} if current input_label(id, name, description, Array === default ? default * ", " : nil, extra) + choices.collect do |choice| choice_name = name.to_s + "[#{ choice }]" check_true = (current && current.include?(choice.to_s)) || (default && default.include?(choice.to_s)) check_true = false if check_true.nil? check_false = ! check_true choice_id = id + "[#{ Misc.snake_case(choice) }]" if id false_id = choice_id + '_false' true_id = choice_id + '_true' else false_id = nil true_id = nil end choice_html = html_tag("input", nil, :type => :checkbox, :checked => check_true, :name => choice_name, :value => "true", :id => choice_id) + input_label(choice_id, choice, choice, nil, extra.merge({:class => :inline})) html_tag('span', choice_html, :class => 'choice') end * "\n" when :boolean current = param2boolean(current) unless current.nil? default = param2boolean(default) unless default.nil? check_true = current.nil? ? default : current check_true = false if check_true.nil? check_false = ! check_true if id false_id = id + '__' << 'false' true_id = id + '__' << 'true' else false_id = nil true_id = nil end input_label(id, name, description, default, extra) + html_tag("input", nil, :type => :hidden, :name => name.to_s + "_checkbox_false", :value => "false") + html_tag("input", nil, :type => :checkbox, :checked => check_true, :name => name, :value => "true", :id => id) when :string, :float, :integer value = current.nil? ? default : current input_type = type == :string ? "text" : "number" step = case type when :string nil when :float "any" when :integer 1 end input_label(id, name, description, default, extra) + html_tag("input", nil, .merge(:type => input_type, :name => name, :value => value, :id => id, :step => step)) when :tsv, :array, :text value = current.nil? ? default : current value = value * "\n" if Array === value if no_file input_label(id, name, description, default, extra) + html_tag("textarea", value || "" , :name => name, :id => id ) else input_label(id, name, description, default, extra) + file_or_text_area(id, name, value) end when :select value = current.nil? ? default : current allow_empty = consume_parameter :allow_empty, extra = consume_parameter :select_options, extra if = .collect do |option| option, option_name = option if Array === option option_name = option if option_name.nil? html_tag('option', option_name, :value => option, :selected => option.to_s == value.to_s) end else = [] end .unshift html_tag('option', 'none', :value => 'none', :selected => value.to_s == 'none') if allow_empty input_label(id, name, description, default, extra) + html_tag('select', * "\n", .merge(:name => name, :id => id, "attr-selected" => (value ? value.to_s : ""))) else "<span> Unsupported input #{name} #{type} </span>" end end |
#fragment(link = nil, &block) ⇒ Object
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 |
# File 'lib/rbbt/rest/common/render.rb', line 69 def fragment(link = nil, &block) fragment_code, link = [link.to_s, nil] if link and not link.to_s[0] == '<' if block_given? if defined? @step and cache_type == :asynchronous or cache_type == :async fragment_code ||= (rand * 100000).to_i.to_s fragment_file = @step.file(fragment_code) pid = @step.child{ begin res = capture_haml &block Open.write(fragment_file, res) rescue Exception Open.write(fragment_file + '.error', $!.) Open.write(fragment_file + '.backtrace', $!.backtrace * "\n") if $!.backtrace raise $!. end } url = request.fullpath url = remove_GET_param(url, "_update") url = remove_GET_param(url, "_") fragment_url = add_GET_param(url, "_fragment", fragment_code) if link.nil? html_tag('a', "", :href => fragment_url, :class => 'fragment') else if link =~ / href=/ link.sub(/ href=('|")/," href='#{fragment_url}'") else link.sub(/<a /,"<a href='#{fragment_url}' ") end end else yield nil end else if link =~ / class=/ link.sub(/ class=('|")/,' class=\1fragment ') else link.sub(/<a /,'<a class="fragment" ') end end end |
#hash2dl(hash, options = {}) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/rbbt/rest/common/misc.rb', line 183 def hash2dl(hash, = {}) entries = hash.collect{|k,v| v = v * ", " if Array === v html_tag(:dt, k) + html_tag(:dd, v) } * "\n" html_tag(:dl, entries, ) end |
#header(field, entity_type, entity_options = {}) ⇒ Object
213 214 215 216 |
# File 'lib/rbbt/rest/common/table.rb', line 213 def header(field, entity_type, = {}) @table_headers ||= {} @table_headers[field] = [entity_type, ] end |
#html_tag(*args) ⇒ Object
167 168 169 |
# File 'lib/rbbt/rest/common/misc.rb', line 167 def html_tag(*args) Misc.html_tag(*args) end |
#input_label(id, name, description = nil, default = nil, options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/rbbt/rest/common/forms.rb', line 5 def input_label(id, name, description = nil, default = nil, = {}) ||= {} text = consume_parameter :label, if text.nil? text = name.to_s text += html_tag('span', " (default: " << default.to_s << ")", :class => 'input_default') unless default.nil? end label_id = id.nil? ? nil : 'label_for__' << id html_tag('label', text, {:id => label_id, :for => id, :title => description}.merge()) end |
#link_css(file) ⇒ Object
22 23 24 25 26 |
# File 'lib/rbbt/rest/common/resources.rb', line 22 def link_css(file) file += '.css' unless file =~ /.css$/ file << "?_update=reload" if @debug_css html_tag('link', nil, :rel => 'stylesheet', :type => 'text/css', :href => file) end |
#link_js(file) ⇒ Object
28 29 30 31 |
# File 'lib/rbbt/rest/common/resources.rb', line 28 def link_js(file) file += '.js' unless file =~ /.js$/ html_tag('script', " ", :src => file, :type => 'text/javascript') end |
#load_tsv(file) ⇒ Object
256 257 258 |
# File 'lib/rbbt/rest/common/table.rb', line 256 def load_tsv(file) RbbtRESTHelpers.load_tsv(file) end |
#locate_javascript(script) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/rbbt/rest/common/locate.rb', line 73 def locate_javascript(script) javascript_resources.each do |resource| path = locate_javascript_from_resource(resource, script) return path if path.exists? end raise TemplateMissing, "Script #{ script } not found" end |
#locate_javascript_from_resource(resource, script) ⇒ Object
67 68 69 70 71 |
# File 'lib/rbbt/rest/common/locate.rb', line 67 def locate_javascript_from_resource(resource, script) path = resource[script + '.js'] return path if path.exists? resource[script] end |
#locate_sass(template) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/rbbt/rest/common/locate.rb', line 47 def locate_sass(template) sass_resources.each do |resource| path = locate_sass_from_resource(resource, template) return path if path.exists? end raise TemplateMissing, "Sass template #{ template } not found" end |
#locate_sass_from_resource(resource, template) ⇒ Object
43 44 45 |
# File 'lib/rbbt/rest/common/locate.rb', line 43 def locate_sass_from_resource(resource, template) resource[template + '.sass'] end |
#locate_template(template) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/rbbt/rest/common/locate.rb', line 20 def locate_template(template) template_resources.each do |resource| path = locate_template_from_resource(resource, template) return path if path.exists? end raise TemplateMissing, "Template #{ template } not found" end |
#locate_template_from_resource(resource, template) ⇒ Object
16 17 18 |
# File 'lib/rbbt/rest/common/locate.rb', line 16 def locate_template_from_resource(resource, template) resource[template + '.haml'] end |
#log(status, message) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/rbbt/rest/common/misc.rb', line 8 def log(status, ) if @step @step.log(status, ) else Log.debug("[#{ status }] #{ }") end end |
#modal_fragment(text, &block) ⇒ Object
113 114 115 116 |
# File 'lib/rbbt/rest/common/render.rb', line 113 def modal_fragment(text, &block) link = "<a class='modal_fragment'>#{text}</a>" fragment(link, &block) end |
#old_cache(path, check) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/rbbt/rest/common/cache.rb', line 9 def old_cache(path, check) return false if check.nil? or check.empty? return false if not File.exists? path check = [check] unless Array === check return check.select{|file| not File.exists?(file) or File.mtime(file) > File.mtime(path)}.any? end |
#paginate(object, page = nil, just_keys = false) ⇒ Object
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 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rbbt/rest/common/table.rb', line 97 def paginate(object, page = nil, just_keys = false) return object unless TSV === object and not page.nil? return object if page == "all" or page.nil? num, size, field = parse_page(page) if field and field[0] == "-"[0] field = field[1..-1] reverse = true else reverse = false end field = CGI.unescapeHTML(Entity::REST.restore_element(field)) if object.entity_templates[field] entity = object.entity_templates[field].annotation_types.last else entity = Entity.formats[field] end num = num.to_i size = size.to_i max = object.size / size + 1 num = max if num > max num = - max if num < - max if entity and entity.respond_to? :tsv_sort object.page(num, size, field, just_keys, reverse, &entity.method(:tsv_sort)) else object.page(num, size, field, just_keys, reverse) end end |
#param2boolean(value) ⇒ Object
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rbbt/rest/common/misc.rb', line 156 def param2boolean(value) case value when "true", "True", "TRUE", "T", "yes", "Yes", "y", "Y" true when "false", "False", "FALSE", "F", "no", "No", "N", "n" false else value end end |
#parse_page(page) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rbbt/rest/common/table.rb', line 86 def parse_page(page) num, size, field = page.split("~").values_at 0, 1, 2 field, size = size, nil if field.nil? field = "key" if field.nil? or field.empty? size = PAGE_SIZE if size.nil? or size.empty? [num, size, field] end |
#partial_render(template, locals = {}, cache = nil, cache_options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/rbbt/rest/common/render.rb', line 64 def partial_render(template, locals = {}, cache = nil, = {}) template_file = locate_template(template) render(template_file, locals, nil, cache, ) end |
#permalink(path) ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/rbbt/rest/common/misc.rb', line 192 def permalink(path) id = Misc.digest(Time.now.to_s) dest = File.join(settings.permalink_dir, id) FileUtils.mkdir_p settings.permalink_dir unless File.exists? settings.permalink_dir FileUtils.ln_s(path, dest) "/permalink/#{ id }" end |
#process_common_parameters ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rbbt/rest/common/misc.rb', line 24 def process_common_parameters @ajax = request.xhr? @ajax_url = headers["AJAX-URL"] @fullpath = request.fullpath @layout = consume_parameter(:_layout) @layout = false if @layout.nil? and @ajax @layout = true if @layout.nil? and not @ajax @layout = false if @layout == "false" @layout = true if @layout == "true" @format = consume_parameter(:_format) @format = :html if @format.nil? @format = @format.to_sym if String === @format @size = consume_parameter(:_size) @size = @size.to_sym if String === @size @update = consume_parameter(:_update) unless @update @update = @update.to_sym if String === @update @profile = consume_parameter(:_profile) unless @profile @profile = @profile.to_sym if String === @profile @cache_type = consume_parameter(:_cache_type) @cache_type = @cache_type.to_sym if String === @cache_type @debug_js = consume_parameter(:_debug_js) @debug_js = false if @debug_js.nil? or @debug_js == "false" @debug_css = consume_parameter(:_debug_css) @debug_css = false if @debug_css.nil? or @debug_css == "false" @_ = consume_parameter(:_) @fragment = consume_parameter(:_fragment) @excel_use_name = consume_parameter(:_excel_use_name) @excel_sort_by = consume_parameter(:_excel_sort_by) @excel_sort_by_cast = consume_parameter(:_excel_sort_by_cast) @splat = consume_parameter :splat @captures = consume_parameter :captures # TSV table pagination, filtering, and slicing @page = consume_parameter :_page @filter = consume_parameter :_filter @column = consume_parameter :_column # Fix boolean inputs sumbitted using checkboxes params.keys.each do |param| if param =~ /(.*)_checkbox_false$/ params[$1] = false if params[$1].nil? params.delete param end end @permalink = consume_parameter :_permalink @clean_params = params.dup end |
#production? ⇒ Boolean
20 21 22 |
# File 'lib/rbbt/rest/common/misc.rb', line 20 def production? ENV["RACK_ENV"] == "production" end |
#record_css(file) ⇒ Object
18 19 20 |
# File 'lib/rbbt/rest/common/resources.rb', line 18 def record_css(file) recorded_css_files << file end |
#record_js(file) ⇒ Object
14 15 16 |
# File 'lib/rbbt/rest/common/resources.rb', line 14 def record_js(file) recorded_js_files << file end |
#recorded_css_files ⇒ Object
9 10 11 |
# File 'lib/rbbt/rest/common/resources.rb', line 9 def recorded_css_files @recorded_css_files ||= [] end |
#recorded_js_files ⇒ Object
5 6 7 |
# File 'lib/rbbt/rest/common/resources.rb', line 5 def recorded_js_files @recorded_js_files ||= [] end |
#remove_GET_param(url, param) ⇒ Object
171 172 173 |
# File 'lib/rbbt/rest/common/misc.rb', line 171 def remove_GET_param(url, param) url.gsub(/&?#{param}=[^&]+/,'').sub(/\?$/, '') end |
#render(template_file, locals = {}, layout_file = nil, cache = nil, cache_options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rbbt/rest/common/render.rb', line 35 def render(template_file, locals = {}, layout_file = nil, cache = nil, = {}) raise TemplateMissing, "Template #{ template_file } not found" unless template_file.exists? raise TemplateMissing, "Template #{ layout_file } not found" unless layout_file.nil? or layout_file.exists? if layout_file Haml::Engine.new(Open.read(layout_file), :filename => layout_file, :ugly => production?).render(self, locals) do cache(cache, locals.merge(:_template_file => template_file, :user => user).merge()) do Log.debug("Rendering #{template_file} with layout") Haml::Engine.new(Open.read(template_file), :filename => template_file, :ugly => production?).render(self, locals) end end else cache(cache, locals.merge(:_template_file => template_file, :user => user).merge()) do Log.debug("Rendering #{template_file} without layout") Haml::Engine.new(Open.read(template_file), :filename => template_file, :ugly => production?).render(self, locals) end end end |
#render_partial(template_file, locals = {}, cache = nil, cache_options = {}) ⇒ Object
53 54 55 |
# File 'lib/rbbt/rest/common/render.rb', line 53 def render_partial(template_file, locals = {}, cache = nil, = {}) render(template_file, locals, nil, cache, ) end |
#resource(filename = nil, text = nil, type = nil, options = {}) {|f| ... } ⇒ Object
118 119 120 121 122 123 124 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 |
# File 'lib/rbbt/rest/common/render.rb', line 118 def resource(filename = nil, text = nil, type = nil, = {}) case when filename.nil? filename = File.basename(TmpFile.tmp_file) when filename[0] == "."[0] extension = filename filename = File.basename(TmpFile.tmp_file) + extension end text ||= filename filename = Misc.sanitize_filename(filename) f = File.join(settings.file_dir, filename) FileUtils.mkdir_p(File.dirname(f)) yield(f) type ||= :link case type when :image "<img src='/files/#{ filename }' class='file_resource'/>" when :link "<a href='/files/#{ filename }' class='file_resource'>#{ text }</a>" when :linked_image "<a href='/files/#{ filename }' class='file_resource' target='_blank'><img src='/files/#{ filename }' class='file_resource'/></a>" when :zoomable_image id = [:id] || Misc.digest(filename) width, height= [600, 600] "<div class='zoomable_image'><img id='#{id}' style='width:#{width}px; height:#{height}px' rel='/files/#{ filename }' src='/files/#{ filename }' class='file_resource'/></div>" when :mapped_image mapid = [:mapid] || [:id] + '_map' width, height= [300, 300] mapfile = f.sub(/\.[^.]+$/, '.html') "<div class='mapped_image'>#{Open.read(mapfile)}<img class='has_map' usemap='##{mapid}' rel='/files/#{ filename }' src='/files/#{ filename }' class='file_resource'/></div>" else raise "Type not understood: #{ type }" end end |
#reveal(text, id = nil, &block) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rbbt/rest/common/render.rb', line 156 def reveal(text, id = nil, &block) id ||= "rbbt_reveal_" << (rand * 10000).to_i.to_s content_html = capture_haml(&block) str = html_tag('a', text.to_s, :href => "#", "data-reveal-id" => 'modal1', 'attr-reveal_id' => id, 'class' => 'rbbt_reveal_trigger') << "\n" << html_tag('div', "\n" << content_html << "\n", :id => id, 'class' => 'rbbt_reveal_content') << "\n" str end |
#save_tsv(file) ⇒ Object
251 252 253 |
# File 'lib/rbbt/rest/common/table.rb', line 251 def save_tsv(file) RbbtRESTHelpers.save_tsv(file) end |
#serve_css ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/rbbt/rest/common/resources.rb', line 68 def serve_css res = recorded_css_files.collect{|file| link_css(file) } * "\n" recorded_css_files.clear res end |
#serve_js(compress = true) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rbbt/rest/common/resources.rb', line 33 def serve_js(compress = true) if production? and compress and not @debug_js md5 = Misc.digest(recorded_js_files * ",") filename = File.join(settings.file_dir, "all_js-#{md5}.js") if not File.exists?(filename) Log.debug("Regenerating JS Compressed file: #{ filename }") text = recorded_js_files.collect{|file| begin path = locate_javascript(file) rescue path = locate_javascript(file.split("/")[2..-1] * "/") end Open.read(path) } * "\n" FileUtils.mkdir_p File.dirname(filename) unless File.exists? File.dirname(filename) Open.write(filename, Uglifier.compile(text)) end res = "<script src='/files/#{File.basename(filename)}' type='text/javascript'></script>" else res = recorded_js_files.collect{|file| link_js(file) } * "\n" end recorded_js_files.clear res end |
#table(options = {}) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/rbbt/rest/common/table.rb', line 260 def table( = {}) = {} if .nil? tsv = yield table_code = [:table_id] || (rand * 100000).to_i.to_s table_code = Entity::REST.clean_element(table_code) table_code.sub!(/[^\w]/,'_') table_file = @step.file(table_code) url = add_GET_param(@fullpath, "_fragment", File.basename(table_file)) url = remove_GET_param(url, "_update") url = remove_GET_param(url, "_") table_class = [] table_class << 'wide responsive' if tsv.fields.length > 4 [:url] = url [:table_class] = table_class [:tsv_entity_options] = tsv. if @table_headers and @table_headers.any? [:headers] = @table_headers @table_headers = {} end if tsv.entity_templates and tsv.entity_templates.any? [:headers] ||= {} tsv.entity_templates.each do |field,template| next if [:headers].include? field next if template.nil? info = template.info info.delete :format info.delete :annotation_types info.delete :annotated_array [:headers][field] = [template.annotation_types.last.to_s, info] end end if @table_filters and @table_filters.any? [:filters] = @table_filters @table_filters = {} end Open.write table_file, tsv.to_s Open.write table_file + '.table_options', .to_yaml if defined? .any? total_size = tsv.size if [:page].nil? and total_size > PAGE_SIZE * 1.2 @page = "1" else @page = [:page] end tsv2html(table_file, ) end |
#table_value(value, type = nil, options = {}) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rbbt/rest/common/table.rb', line 200 def table_value(value, type = nil, = {}) = {} if .nil? return value.list_link :length, [:list_id] if Array === value and [:list_id] and value.respond_to? :list_link = [:entity_options] Misc.prepare_entity(value, type, ) if Entity.formats.include? type and not [:unnamed] value = value.link if value.respond_to? :link Array === value ? value.collect{|v| v.to_s} * ", " : value end |
#template_render(template, locals = {}, cache = nil, cache_options = {}) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rbbt/rest/common/render.rb', line 57 def template_render(template, locals = {}, cache = nil, = {}) template_file = locate_template(template) layout_file = layout ? locate_template("layout") : nil render(template_file, locals, layout_file, cache, ) end |
#tsv2html(file, default_table_options = {}) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/rbbt/rest/common/table.rb', line 320 def tsv2html(file, = {}) if TSV === file tsv, = file, {} [:unnamed] = tsv.unnamed else tsv, = load_tsv(file) end content_type "text/html" rows, length = tsv_rows(tsv) = .merge() partial_render('partials/table', {:total_size => length, :rows => rows, :header => tsv.all_fields, :table_options => }) end |
#tsv_process(tsv, filter = nil, column = nil) ⇒ Object
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 |
# File 'lib/rbbt/rest/common/table.rb', line 133 def tsv_process(tsv, filter = nil, column = nil) filter = @filter if filter.nil? column = @column if column.nil? if filter and filter.to_s != "false" filter.split(";;").each do |f| key, value = f.split("~") next if value.nil? or value.empty? value = Entity::REST.restore_element(value) #invert if value =~ /^!\s*(.*)/ value = $1 invert = true else invert = false end #name if value =~ /^:name:\s*(.*)/ value = $1 name = true else name = false end if name old_tsv = tsv tsv = tsv.reorder(:key, key).add_field "NAME" do |k,values| NamedArray === values ? values[key].name : values.name end key = "NAME" end case when value =~ /^([<>]=?)(.*)/ tsv = tsv.select(key, invert){|k| k = k.first if Array === k; k.to_f.send($1, $2.to_f)} when value =~ /^\/(.+)\/.{0,2}\s*$/ tsv = tsv.select({key => Regexp.new($1)}, invert) when (value =~ /^\d+$/ and tsv.type == :double or tsv.type == :flat) tsv = tsv.select({key => value.to_i}, invert) else tsv = tsv.select({key => value}, invert) end tsv = old_tsv.select(tsv.keys) if name end end tsv = tsv.column(column) if column and not column.empty? tsv end |
#tsv_rows(tsv, page = nil, filter = nil, column = nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rbbt/rest/common/table.rb', line 188 def tsv_rows(tsv, page = nil, filter = nil, column = nil) tsv = tsv_process(tsv, filter, column) length = tsv.size page = @page if page.nil? if page.nil? or page.to_s == "false" [tsv_rows_full(tsv), length] else [tsv_rows_full(paginate(tsv, page)), length] end end |
#tsv_rows_full(tsv) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbbt/rest/common/table.rb', line 70 def tsv_rows_full(tsv) case tsv.type when :single tsv.collect{|id, value| [id, value]} when :list key_field = tsv.key_field tsv.collect{|id, values| new_values = [id].concat(values); begin NamedArray.setup(new_values, values.fields, id, values., values.entity_templates); new_values.fields = [key_field].concat values.fields end if values.respond_to? :fields; new_values } when :flat key_field = tsv.key_field tsv.collect{|id, values| [id, values]} when :double key_field = tsv.key_field tsv.collect{|id, value_lists| new_value_lists = [id].concat(value_lists); begin NamedArray.setup(new_value_lists, value_lists.fields, id, value_lists., value_lists.entity_templates); new_value_lists.fields = ([key_field].concat value_lists.fields) end if value_lists.respond_to? :fields; new_value_lists } end end |
#wait_on(job, layout = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rbbt/rest/common/render.rb', line 19 def wait_on(job, layout = nil) 3.times do sleep 1 end raise RbbtRESTHelpers::Retry if job.done? or job.error? layout = @layout if layout.nil? layout_file = (layout ? locate_template('layout') : nil) template_file = locate_template('wait') status 202 render template_file, {:job => job}, layout_file end |