Module: Entity::REST

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

Constant Summary collapse

LIST_REPO =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_list(id) ⇒ Object



49
50
51
# File 'lib/rbbt/workflow/rest/entity.rb', line 49

def self.load_list(id)
  Entity::REST::LIST_REPO[id]
end

.save_list(id, list) ⇒ Object



45
46
47
# File 'lib/rbbt/workflow/rest/entity.rb', line 45

def self.save_list(id, list)
  Entity::REST::LIST_REPO[id] = list
end

Instance Method Details



72
73
74
75
76
77
78
79
# File 'lib/rbbt/workflow/rest/entity.rb', line 72

def action_link(action, text = nil, inputs = {})
  options = inputs.delete(:link_options) || {}
  clean_inputs = {}
  inputs.each do |k, v| 
    clean_inputs[k] = Array === v ? v * "," : v
  end
  link(text, nil, options.merge(:extra_params => clean_inputs, :action => action))
end


6
7
8
9
10
11
12
13
14
15
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
# File 'lib/rbbt/workflow/rest/entity.rb', line 6

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)}
    else
      self.collect{|e| e.link(text, url, options)}
    end
  else

    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)? 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

    type = self.respond_to?(:format)? annotation_types.last.to_s + 
      ":" + 
      (self.format || "") : annotation_types.last.to_s

    params = options[:params] || self.info.reject{|k,v| [:annotation_types, :format].include?(k)}
    params.merge! options[:extra_params] if options.include? :extra_params

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

    annotations_str = params.collect{|k,v| [k, CGI.escape(v.to_s)] * "="} * "&"
    "<a class='#{klass}' href='#{url + (annotations_str.any? ? '?' + annotations_str : "")}'>#{text}</a>"
  end
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rbbt/workflow/rest/entity.rb', line 81

def list_action_link(action, text = nil, id = nil, options = {})
  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

  Entity::REST.save_list(id, self)

  klass = self.respond_to?(:format)? 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

  type = self.respond_to?(:format)? annotation_types.last.to_s + ":" + self.format : annotation_types.last.to_s

  annotations_str = self.info.reject{|k,v| [:annotation_types, :format].include?(k)}.collect{|k,v| [k, CGI.escape(v.to_s)] * "="} * "&"

  "<a class='entity_list_action #{klass}' href='#{File.join("/entity_list_action", type, action, id) + (annotations_str.any? ? '?' + annotations_str : "")}'>#{text}</a>"
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbbt/workflow/rest/entity.rb', line 53

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

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

  Entity::REST.save_list(id, self)

  klass = self.respond_to?(:format)? 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

  type = self.respond_to?(:format)? annotation_types.last.to_s + ":" + self.format : annotation_types.last.to_s

  annotations_str = self.info.reject{|k,v| [:annotation_types, :format].include?(k)}.collect{|k,v| [k, CGI.escape(v.to_s)] * "="} * "&"

  "<a class='entity_list #{klass}' href='#{File.join("/entity_list", type, id) + (annotations_str.any? ? '?' + annotations_str : "")}'>#{text}</a>"
end