Module: Entity::REST

Includes:
RbbtHTMLHelpers
Defined in:
lib/rbbt/workflow/rest/entity.rb

Constant Summary

Constants included from RbbtHTMLHelpers

RbbtHTMLHelpers::PAGESIZE

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RbbtHTMLHelpers

#add_user_defaults, #clean_list_id, #clear_user_defaults, #entity_action_render, #entity_list_action_render, #entity_list_render, #entity_render, #followed_users, #form_input, #format_float, #html_options_str, #included, #link_to_resource, #locate_file, locate_file, #log, #paginate, #parameter_controls, #parse_page, #process_result, #record_css, #record_js, #redirect_to_entity, #save_user_defaults, #serve_css, #serve_js, #setup_entity, #table, #user_action_defaults, #workflow_partial, #workflow_render, #workflow_render_result

Class Attribute Details

.list_repoObject

Returns the value of attribute list_repo.



15
16
17
# File 'lib/rbbt/workflow/rest/entity.rb', line 15

def list_repo
  @list_repo
end

Class Method Details

.delete_list(entity_type, id, user) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rbbt/workflow/rest/entity.rb', line 118

def self.delete_list(entity_type, id, user)
  path = list_file(entity_type, id, user)
  "This list does not belong to #{ user }: #{[entity_type, id] * ": "}" unless File.exists? path
  Misc.lock path do
    begin
      FileUtils.rm path if File.exists? path
    rescue
      raise $!
    end
  end
end

.list_file(entity_type, id, user = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rbbt/workflow/rest/entity.rb', line 61

def self.list_file(entity_type, id, user = nil)
  entity_type = entity_type.split(":").first if not entity_type.nil? and entity_type.index ':'

  id = CGI.unescape(id)
  id = Misc.sanitize_filename(id)
  raise "Ilegal list id: #{ id }" unless Misc.path_relative_to ".", File.join('.', id)

  if user.nil?
    File.join(Entity.entity_list_cache, entity_type.to_s, id)
  else
    File.join(Entity.entity_list_cache, user.to_s, entity_type.to_s, id)
  end
end

.list_files(user) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rbbt/workflow/rest/entity.rb', line 75

def self.list_files(user)
  lists = {}
  Dir.glob(File.join(Entity.entity_list_cache, user, '*', '*')).each do |file|
    next if File.directory? file

    file = File.expand_path(file)
    raise "Ilegal path: #{ file }. Not relative to #{File.expand_path('.')}" unless Misc.path_relative_to(File.expand_path('.'), file)

    user, entity_type, list = file.split("/")[-3..-1]
    lists[entity_type] ||= []
    lists[entity_type] << list
  end
  lists
end

.load_list(entity_type, id, user = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rbbt/workflow/rest/entity.rb', line 90

def self.load_list(entity_type, id, user = nil)
  path = list_file(entity_type, id, user)
  path = list_file(entity_type, id, :public) unless path != nil and File.exists? path
  path = list_file(entity_type, id) unless path != nil and File.exists? path

  begin
    list = Annotated.load_tsv TSV.open(path)
    list.extend AnnotatedArray
    list
  rescue
    nil
  end
end

.save_list(entity_type, id, list, user = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbbt/workflow/rest/entity.rb', line 104

def self.save_list(entity_type, id, list, user = nil)
  path = list_file(entity_type, id, user)

  Misc.lock path do
    begin
      Open.write(path, Annotated.tsv(list, :all).to_s)
    rescue
      FileUtils.rm path if File.exists? path
      raise $!
    end
  end
end

Instance Method Details



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbbt/workflow/rest/entity.rb', line 155

def action_link(action, text = nil, inputs = {})
  options = inputs.delete(:link_options) || {}
  options[:extra_classes] = inputs.delete(:extra_classes) || inputs.delete("extra_class") if inputs.include?(:extra_classes) or inputs.include?("extra_classes")
  clean_inputs = {}
  inputs.each do |k, v| 
    clean_inputs[k] = Array === v ? v * "," : v
  end

  klass ||= "entity_action " + ((self.respond_to?(:format) and not self.format.nil?) ? self.format.gsub(/\s/,'_') : annotation_types.collect{|t| t.to_s.gsub(/\s/,'_')} * " ")
  klass += " " << options[:extra_classes] if options.include? :extra_classes
  klass += " " << html_class if self.respond_to? :html_class

  link(text, nil, options.merge(:extra_params => clean_inputs, :action => action, :class => klass))
end

#html_table(fields, workflow = nil, field_names = nil) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/rbbt/workflow/rest/entity.rb', line 198

def html_table(fields, workflow = nil, field_names = nil)
  raise "Must be an Array to produce html table: #{self.inspect}" unless Array === self
  rows = self.collect{|entity|
    [entity.id, entity.tsv_values(fields)]
  }

  workflow_partial('partials/_table', workflow = nil, :rows => rows, :header => field_names || fields)
end


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
# File 'lib/rbbt/workflow/rest/entity.rb', line 18

def link(text = nil, url = nil, options = {})
  return nil if self.nil?

  if Array === self
    text ||= self.name if self.respond_to? :name
    if Array === text
      translations = Misc.zip2hash(self, text)
      self.collect{|e| e.link(translations[e], url, options) unless e.nil?}.compact
    else
      self.collect{|e| e.link(text, url, options) unless e.nil?}.compact
    end
  else
    text = nil if String === text and text.empty?
    text ||= self.name if self.respond_to? :name

    text = self if text.nil? or text == :literal or text.to_s.strip.empty?

    klass = options[:class]
    klass ||= "entity " + ((self.respond_to?(:format) and not self.format.nil?) ? self.format.gsub(/\s/,'_') : annotation_types.collect{|t| t.to_s.gsub(/\s/,'_')} * " ")

    klass += " " << options[:extra_classes] || "" if options.include? :extra_classes
    klass += " " << html_class if self.respond_to? :html_class

    main_annotation = options[:entity_type] || annotation_types.select{|a| a.include? Entity::REST}.last.to_s
    entity_type = self.respond_to?(:format)? main_annotation + 
      ":" + 
      CGI.escape(self.format || "") : main_annotation

    params = options[:params] || self.info(true).reject{|k,v| [:annotation_types, :format].include?(k)}
    params.merge! options[:extra_params] if options.include? :extra_params
    params.merge! :entity_id => self.id if annotation_types.select{|annotation_type| annotation_type.respond_to?(:keep_id) and annotation_type.keep_id}.any?

    url ||= if options.include? :action
              File.join("/entity_action", entity_type, options[:action].to_s, self)
            else
              File.join("/entity", entity_type, self)
            end

    annotations_str = params.collect{|k,v| v = v * "|" if Array === v; [k, CGI.escape(v.to_s)] * "="} * "&amp;"
    "<a class='#{klass}' #{rbbt_id} href=\"#{annotations_str.empty? ? url : url + '?' + annotations_str}\" #{options.include?(:html_link_extra_attrs)? options[:html_link_extra_attrs] : "" } title='#{text}'>#{text}</a>"
  end
end


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
# File 'lib/rbbt/workflow/rest/entity.rb', line 170

def list_action_link(action, text = nil, id = nil, options = {})
  reuse = options.delete("reuse") || options.delete(:reuse)

  text = self.length if text.nil? or (String === text and text.strip.empty?)
  id ||= options[:id] || Misc.digest((self * "|").inspect)

  text = id if text == :id

  id = clean_list_id(id)

  klass = (self.respond_to?(:format) and not self.format.nil?) ? self.format.gsub(/\s/,'_') : annotation_types.collect{|t| t.to_s.gsub(/\s/,'_')} * " "

  klass += " " << options.delete(:extra_classes) if options.include? :extra_classes
  klass += " " << html_class if self.respond_to? :html_class
  
  main_annotation = options[:entity_type] || annotation_types.select{|a| a.include? Entity::REST}.last.to_s

  entity_type = self.respond_to?(:format)? main_annotation + 
    ":" + 
    CGI.escape(self.format || "") : main_annotation

  Entity::REST.save_list(main_annotation, id, self) unless reuse

  annotations_str = options.collect{|k,v| [k, CGI.escape(v.to_s)] * "="} * "&amp;"

  "<a class='entity_list_action #{klass}' href='#{File.join("/entity_list_action", entity_type, action, CGI.escape(id)) + (!annotations_str.empty? ? '?' + annotations_str : "")}' title='#{text}'>#{text}</a>"
end


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/workflow/rest/entity.rb', line 130

def list_link(text = nil, id = nil, options = {})
  reuse = options.delete("reuse") || options.delete(:reuse)
  text = self.length if text.nil? or (String === text and text.strip.empty?) or text == :length

  id ||= options[:id] || Misc.digest((self * "|").inspect)

  id = clean_list_id(id)

  klass = (self.respond_to?(:format) and not self.format.nil?) ? self.format.gsub(/\s/,'_') : annotation_types.collect{|t| t.to_s.gsub(/\s/,'_')} * " "

  klass += " " << options[:extra_classes] if options.include? :extra_classes
  klass += " " << html_class if self.respond_to? :html_class

  main_annotation = options[:entity_type] || annotation_types.select{|a| a.include? Entity::REST}.last.to_s
  entity_type = self.respond_to?(:format)? main_annotation + 
    ":" + 
    CGI.escape(self.format || "") : main_annotation


  Entity::REST.save_list(main_annotation, id, self) unless reuse


  "<a class='entity_list #{klass}' href='#{File.join("/entity_list", entity_type, CGI.escape(id))}' title='#{text}'>#{text}</a>"
end

#rbbt_idObject



9
10
11
12
# File 'lib/rbbt/workflow/rest/entity.rb', line 9

def rbbt_id
  return "entity_id='#{self.id}'" unless self.annotations.include? :format and String === self.format
  ([self.format.downcase, '"' + self + '"'] * "=").gsub(/[^"\w=]/,'_')
end