Module: EntityRESTHelpers

Defined in:
lib/rbbt/rest/entity/finder.rb,
lib/rbbt/rest/entity/locate.rb,
lib/rbbt/rest/entity/render.rb,
lib/rbbt/rest/entity/helpers.rb,
lib/rbbt/rest/entity/favourites.rb,
lib/rbbt/rest/entity/action_card.rb,
lib/rbbt/rest/entity/entity_card.rb,
lib/rbbt/rest/entity/list_container.rb,
lib/rbbt/rest/entity/entity_map_card.rb,
lib/rbbt/rest/entity/entity_list_card.rb,
lib/rbbt/rest/entity/action_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entity_resourcesObject



3
4
5
# File 'lib/rbbt/rest/entity/locate.rb', line 3

def self.entity_resources
  @entity_resources ||= []
end

Instance Method Details

#action_card_render(card, &block) ⇒ Object



27
28
29
# File 'lib/rbbt/rest/entity/action_card.rb', line 27

def action_card_render(card, &block)
  partial_render('entity_partials/action_card', :card => card, :block => block)
end

#action_controller_render(controller) ⇒ Object



40
41
42
# File 'lib/rbbt/rest/entity/action_controller.rb', line 40

def action_controller_render(controller)
  partial_render('entity_partials/action_controller', :controller => controller)
end

#action_parameters(values = nil, action_options = {}, form_options = {}, &block) ⇒ Object



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

def action_parameters(values = nil, action_options = {}, form_options = {}, &block)
  o = Object.new
  o.extend AnnotatedModule

  if values.nil?
    values = @clean_params
  else
    values = @clean_params.merge(values)
  end

  o.instance_eval &block

  description = o.description

  inputs = o.inputs || []
  input_types = o.input_types
  input_defaults = o.input_defaults
  input_options = o.input_options
  input_descriptions = o.input_descriptions

  hidden_inputs = []
  inputs.each do |input|
    values[input] = values.delete input.to_s unless values.include? input
    input_value = values[input]
    input_default = input_defaults[input]
    input_option = input_options[input]
  end

  locals = {}
  info = {:inputs => inputs, :input_descriptions => input_descriptions, :input_defaults => input_defaults, :input_options => input_options, :input_types => input_types, :values => values}
  locals[:id] = action_parameters_id
  locals[:action] = @ajax_url
  locals[:klass] = 'action_parameter_form'
  locals[:info] = info
  locals[:description] = description
  locals[:method] = 'GET'
  locals = locals.merge(form_options)

  html_tag :div, partial_render('partials/form', locals), {:class => 'action_parameters ui raised segment'}.merge(action_options)
end

#action_parameters_idObject



20
21
22
23
24
25
26
27
# File 'lib/rbbt/rest/entity/helpers.rb', line 20

def action_parameters_id
  case
  when (params[:entity] and params[:action])
    ["action_params", params[:entity], params[:action]] * "__"
  else
    ["action_params", (rand * 1000).to_i] * "__"
  end
end

#add_favourite_entity(entity) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rbbt/rest/entity/favourites.rb', line 20

def add_favourite_entity(entity)
  raise "You need to login to have favourites" unless authorized?

  entity_type = entity.base_type
  dir = Path.setup(File.join(settings.favourites_dir, user))

  if (file = dir[entity_type]).exists?
    entities = Annotated.load_tsv(TSV.open(file))
    entities << entity
    Open.write(file, Annotated.tsv(entities.uniq, :all).to_s)
  else
    entities = [entity]
    Open.write(file, Annotated.tsv(entities, :all).to_s)
  end
end

#add_favourite_entity_list(entity_type, list) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rbbt/rest/entity/favourites.rb', line 68

def add_favourite_entity_list(entity_type, list)
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_lists_dir, user))

  if (file = dir[entity_type].find).exists?
    lists = Open.read(file.find).split("\n")
    lists << list
    Open.write(file, lists.uniq * "\n")
  else
    lists = [list]
    Open.write(file, lists * "\n")
  end
end

#add_favourite_entity_map(entity_type, column, map) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbbt/rest/entity/favourites.rb', line 118

def add_favourite_entity_map(entity_type, column, map)
  raise "You need to login to have favourites" unless authorized?

  column = column.gsub('/', '--')
  dir = Path.setup(File.join(settings.favourite_maps_dir, user))

  if (file = dir[entity_type][column]).exists?
    maps = Open.read(file.find).split("\n")
    maps << map
    maps.uniq!
    Open.write(file, maps.uniq * "\n")
  else
    maps = [map]
    Open.write(file, maps * "\n")
  end
end

#default_action_controller(entity, list_id = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbbt/rest/entity/action_controller.rb', line 20

def default_action_controller(entity, list_id = nil)
  action_controller = EntityActionController.new(entity, list_id)

  case
  when Array === entity
    find_all_entity_list_action_templates(entity, true).each do |action|
      action_controller.add action, Misc.humanize(action, :format => :sentence), :reuse => true
    end
  when TSV === entity
    find_all_entity_map_action_templates(entity, true).each do |action|
      action_controller.add action, Misc.humanize(action, :format => :sentence), :reuse => true
    end
  else
    find_all_entity_action_templates(entity, true).each do |action|
      action_controller.add action, Misc.humanize(action, :format => :sentence)
    end
  end
  action_controller
end

#entity_action_render(entity, action, params = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbbt/rest/entity/render.rb', line 50

def entity_action_render(entity, action, params = {})
  template_file = locate_entity_action_template(entity, action)

  locals = params.merge({:entity => entity})

  name = entity.respond_to?(:name)? entity.name : entity
  @title = "#{action} #{name} [#{$title}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action }: #{ entity }")
end

#entity_card_render(card, &block) ⇒ Object



23
24
25
# File 'lib/rbbt/rest/entity/entity_card.rb', line 23

def entity_card_render(card,&block)
  partial_render('entity_partials/entity_card', :card => card, :block => block)
end

#entity_list_action_render(list, action, id, params = {}) ⇒ Object



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

def entity_list_action_render(list, action, id, params = {})
  template_file = locate_entity_list_action_template(list, action)

  locals = params.merge({:list => list, :list_id => id})

  name = id
  @title = "#{action} #{name} [#{$title}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for list: #{ id }")
end

#entity_list_card_render(card) ⇒ Object



23
24
25
# File 'lib/rbbt/rest/entity/entity_list_card.rb', line 23

def entity_list_card_render(card)
  partial_render('entity_partials/entity_list_card', :card => card)
end

#entity_list_render(list, id) ⇒ Object



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

def entity_list_render(list, id)
  template_file = locate_entity_list_template(list)

  locals = {:list => list, :list_id => id}

  name = id
  @title = "#{name} [#{$title}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity list: #{ id }")
end

#entity_map_action_render(map, action, id, params = {}) ⇒ Object



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

def entity_map_action_render(map, action, id, params = {})
  template_file = locate_entity_map_action_template(map, action)

  locals = params.merge({:map => map, :map_id => id})

  name = id
  @title = "#{action} #{name} [#{$title}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for map: #{ id }")
end

#entity_map_card_render(card) ⇒ Object



12
13
14
# File 'lib/rbbt/rest/entity/entity_map_card.rb', line 12

def entity_map_card_render(card)
  partial_render('entity_partials/entity_map_card', :card => card)
end

#entity_map_render(map_id, type, column) ⇒ Object



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

def entity_map_render(map_id, type, column)
  template_file = locate_entity_map_template(type, column)

  map = Entity::Map.load_map(type, column, map_id, user)
  locals = {:map => map, :map_id => map_id}

  name = map_id
  @title = "#{name} [#{$title}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity map: #{ map_id }")
end

#entity_render(entity, params = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbbt/rest/entity/render.rb', line 36

def entity_render(entity, params = {})
  template_file = locate_entity_template(entity)

  params = {} if params.nil?
  locals = {:entity => entity}.merge(params)

  name = entity.respond_to?(:name)? entity.name : entity
  @title = "#{name} [#{$title}]"

  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity: #{ entity }")
end

#entity_resourcesObject



7
8
9
# File 'lib/rbbt/rest/entity/locate.rb', line 7

def entity_resources
  [Rbbt.share.views.find(:lib)] +  EntityRESTHelpers.entity_resources
end

#favourite_entitiesObject

{{{ Entities



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rbbt/rest/entity/favourites.rb', line 5

def favourite_entities
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourites_dir, user))

  favourites = {}
  dir.glob('**').each do |file|
    type = File.basename(file)
    entities = Annotated.load_tsv(TSV.open(file))
    favourites[type] = entities
  end

  favourites
end

#favourite_entity_listsObject

{{{ Entity Lists



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rbbt/rest/entity/favourites.rb', line 55

def favourite_entity_lists
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_lists_dir, user))
  favourites = {}
  dir.glob('**').each do |file|
    type = File.basename(file)
    lists = Open.read(file).split("\n")
    favourites[type] = lists
  end
  favourites
end

#favourite_entity_mapsObject

{{{ Entity Maps



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

def favourite_entity_maps
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_maps_dir, user))
  favourites = {}
  dir.find.glob('*').each do |type_dir|
    type = File.basename(type_dir)
    Path.setup(type_dir).glob('*').each do |file|
      column = File.basename(file).gsub('--', '/')
      maps = Open.read(file).split("\n")
      favourites[type] ||= {}
      favourites[type][column] = maps
    end
  end
  favourites
end

#find_all_entity_action_templates(entity, check = false) ⇒ Object



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

def find_all_entity_action_templates(entity, check = false)
  paths = []

  if entity.respond_to? :dir and Path === entity.dir
    paths.concat find_all_entity_action_templates_from_resource(entity.dir.www.views, entity)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_action_templates_from_resource(resource, entity)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_action_templates_from_resource(resource, "Default")
  end

  if check
    paths = paths.reject do |path|
      (path.basename == "edit.haml" or path.basename == 'new.haml') 
    end
  end

  actions = paths.collect{|file| file.basename.sub('.haml', '') }.uniq

  actions.select! do |action|
    begin
      locate_entity_action_template(entity, action)
    rescue Exception
      false
    end
  end if check
  
  actions
end

#find_all_entity_action_templates_from_resource(resource, entity) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/rbbt/rest/entity/locate.rb', line 124

def find_all_entity_action_templates_from_resource(resource, entity)
  if entity == "Default" 
    resource.entity["Default"].glob("*.haml").sort
  else
    entity.annotation_types.collect do |annotation|
      resource.entity[annotation].glob('*.haml')
    end.compact.flatten.sort
  end
end

#find_all_entity_list_action_templates(list, check = false) ⇒ Object



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

def find_all_entity_list_action_templates(list, check = false)
  paths = []

  if list.respond_to? :dir and Path === list.dir
    paths.concat find_all_entity_list_action_templates_from_resource(list.dir.www.views, list)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_list_action_templates_from_resource(resource, list)
  end

  entity_resources.each do |resource|
    paths.concat find_all_entity_list_action_templates_from_resource(resource, "Default")
  end

  if check
    paths = paths.reject do |path|
      (path.basename == "edit.haml" or path.basename == 'new.haml') 
    end
  end

  actions = paths.collect{|file| file.basename.sub('.haml', '') }.uniq

  actions.select! do |action|
    begin
      locate_entity_list_action_template(list, action)
    rescue Exception
      false
    end
  end if check
  
  actions
end

#find_all_entity_list_action_templates_from_resource(resource, entity) ⇒ Object



255
256
257
258
259
260
261
262
263
264
# File 'lib/rbbt/rest/entity/locate.rb', line 255

def find_all_entity_list_action_templates_from_resource(resource, entity)

  if entity == "Default" 
    resource.entity_list["Default"].glob("*.haml").sort
  else
    entity.annotation_types.collect do |annotation|
      resource.entity_list[annotation].glob('*.haml')
    end.compact.flatten.sort
  end
end

#find_all_entity_map_action_templates(map, check = false) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/rbbt/rest/entity/locate.rb', line 381

def find_all_entity_map_action_templates(map, check = false)
  paths = []

  entity_resources.each do |resource|
    paths.concat find_all_entity_map_action_templates_from_resource(resource, map)
  end

  if check
    paths = paths.reject do |path|
      (path.basename == "edit.haml" or path.basename == 'new.haml')
    end
  end

  actions = paths.collect{|file| file.basename.sub('.haml', '') }.uniq

  actions.select! do |action|
    begin
      locate_entity_map_action_template(map, action)
    rescue Exception
      false
    end
  end if check
  
  actions
end

#find_all_entity_map_action_templates_from_resource(resource, map) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/rbbt/rest/entity/locate.rb', line 365

def find_all_entity_map_action_templates_from_resource(resource, map)
  field = map.key_field

  if map.entity_templates[field] 
    annotation_types = map.entity_templates[field].annotation_types
  else
    annotation_types = [Entity.formats[field]].compact
  end

  annotation_types += ["Default"]

  annotation_types.collect do |annotation|
    resource.entity_map[annotation].glob('*.haml')
  end.compact.flatten
end

#find_entity_url(term) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbbt/rest/entity/finder.rb', line 33

def find_entity_url(term)
  if term =~ /(.*) \[(.*)\]$/
    term = $1
    namespace, format = $2.split(":")
    format, namespace = namespace, nil if format.nil?

    Entity::REST.entity_url(term, format, :organism => namespace)
  else
    sorted_results = finder_find(term)
    organism = sorted_results.select{|r| r[:type] == 'organism' }.first
    sorted_results = sorted_results.reject{|r| r[:type] == 'organism' }
    i = sorted_results.first
    halt 404, "Term not recognized: #{ term }" if i.nil?
    organism_code = i[:namespace]
    organism_code = organism[:code] unless organism.nil? or organism[:code].nil?
    Entity::REST.entity_url(i[:code], i[:format], :organism => organism_code)
  end
end

#finder_find(term) ⇒ Object



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

def finder_find(term)
  return [] unless settings.respond_to? :finder and not settings.finder.nil? and not settings.finder.instances.empty?
  results = settings.finder.find(term)

  results.uniq.collect{|r| 
    info = r.info
    format_string = [info[:namespace], info[:format]].compact * ":"

    info[:code] = r

    if not format_string.empty?
      info[:value] = r + " [#{format_string}]" 
    else
      info[:value] = r
    end

    score = info.delete :score

    if Array === score
      info[:score] = score[0]
      info[:best] = score[1]
    end

    info[:format] ||= info[:type]

    info 
  }.sort_by{|i| i[:score] || 0}.reverse
end

#list(list, list_id = nil, text = nil) ⇒ Object



16
17
18
# File 'lib/rbbt/rest/entity/helpers.rb', line 16

def list(list, list_id = nil, text = nil)
  partial_render('entity_partials/entity_list', :list => list, :list_id => list_id, :text => text)
end

#list_container_render(container) ⇒ Object



20
21
22
# File 'lib/rbbt/rest/entity/list_container.rb', line 20

def list_container_render(container)
  partial_render('entity_partials/list_container', :container => container)
end

#list_hash(list) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/rbbt/rest/entity/helpers.rb', line 4

def list_hash(list)
  return {:entities => list} unless list.respond_to? :base_entity
  info = list.info
  info.delete :annotation_types
  info.delete :annotated_array
  {:entities => list, :info => list.info, :entity_type => list.base_entity}
end

#list_json(list) ⇒ Object



12
13
14
# File 'lib/rbbt/rest/entity/helpers.rb', line 12

def list_json(list)
  list_hash(list).to_json
end

#locate_entity_action_template(entity, action) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbbt/rest/entity/locate.rb', line 103

def locate_entity_action_template(entity, action)

  if entity.respond_to? :dir and Path === entity.dir
    path = locate_entity_action_template_from_resource(entity.dir.www.views, entity, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_action_template_from_resource(resource, entity, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_action_template_from_resource(resource, "Default", action)
    raise "This action was rejected: #{ action }" if reject_template(path,binding)
    return path if path and path.exists?
  end

  raise "Template not found for action #{action}: #{ entity } (#{entity.annotation_types * ", "})"
end

#locate_entity_action_template_from_resource(resource, entity, action) ⇒ Object

{{{ ENTITY ACTION



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

def locate_entity_action_template_from_resource(resource, entity, action)
  if entity == "Default" 
    path = resource.entity["Default"][action.to_s + ".haml"]
    raise "This action was rejected: #{ action }" if path and reject_template(path,binding)
    if path.exists?
      return path
    else
      return nil
    end
  end

  entity.annotation_types.each do |annotation|
    path = resource.entity[annotation][action.to_s + ".haml"]
    raise "This action was rejected: #{ action }" if path and reject_template(path,binding)
    return path if path.exists?
  end

  nil
end

#locate_entity_list_action_template(list, action) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rbbt/rest/entity/locate.rb', line 235

def locate_entity_list_action_template(list, action)

  if list.respond_to? :dir and Path === list.dir
    path = locate_entity_list_action_template_from_resource(list.dir.www.views, list, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_list_action_template_from_resource(resource, list, action)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_list_action_template_from_resource(resource, "Default", action)
    return path if path and path.exists?
  end

  raise "Template not found for list #{ action } (#{list.annotation_types * ", "})"
end

#locate_entity_list_action_template_from_resource(resource, list, action) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rbbt/rest/entity/locate.rb', line 215

def locate_entity_list_action_template_from_resource(resource, list, action)
  if list == "Default" 
    path = resource.entity_list["Default"][action.to_s + ".haml"]
    raise "This action was rejected: #{ action }" if path and reject_template(path,binding)
    if path.exists?
      return path
    else
      return nil
    end
  end

  list.annotation_types.each do |annotation|
    path = resource.entity_list[annotation][action.to_s + ".haml"]
    raise "This action was rejected: #{ action }" if path and reject_template(path,binding)
    return path if path.exists?
  end

  nil
end

#locate_entity_list_template(list) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rbbt/rest/entity/locate.rb', line 187

def locate_entity_list_template(list)

  if list.respond_to? :dir and Path === list.dir
    list_views = list.dir.www.views

    list.annotation_types.each do |annotation|
      path = list_views.entity_list[annotation.to_s + ".haml"]
      return path if path.exists?
    end
  end

  entity_resources.each do |resource|
    path = locate_entity_list_template_from_resource(resource, list)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_list_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for list (#{list.annotation_types * ", "})"
end

#locate_entity_list_template_from_resource(resource, list) ⇒ Object

{{{ ENTITY LIST



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rbbt/rest/entity/locate.rb', line 169

def locate_entity_list_template_from_resource(resource, list)
  if list == "Default" 
    path = resource.entity_list["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  list.annotation_types.each do |annotation|
    path = resource.entity_list[annotation.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#locate_entity_map_action_template(map, action) ⇒ Object



355
356
357
358
359
360
361
362
363
# File 'lib/rbbt/rest/entity/locate.rb', line 355

def locate_entity_map_action_template(map, action)

  entity_resources.each do |resource|
    path = locate_entity_map_action_template_from_resource(resource, map, action)
    return path if path and path.exists?
  end

  raise "Template not found for map #{ action } (#{map.key_field * ", "})"
end

#locate_entity_map_action_template_from_resource(resource, map, action) ⇒ Object

{{{ ENTITY MAP ACTION



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/rbbt/rest/entity/locate.rb', line 335

def locate_entity_map_action_template_from_resource(resource, map, action)
  field = map.key_field

  if map.entity_templates[field] 
    annotation_types = map.entity_templates[field].annotation_types
  else
    annotation_types = [Entity.formats[field]].compact
  end

  annotation_types += ["Default"]

  annotation_types.each do |annotation|
    path = resource.entity_map[annotation][action.to_s + ".haml"]
    raise "This action was rejected: #{ action }" if path and reject_template(path,binding)
    return path if path.exists?
  end

  nil
end

#locate_entity_map_template(type, column) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rbbt/rest/entity/locate.rb', line 318

def locate_entity_map_template(type, column)
  entity_resources.each do |resource|
    path = locate_entity_map_template_from_resource(resource, type)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_map_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for list (#{type}--#{column})"
end

#locate_entity_map_template_from_resource(resource, type) ⇒ Object

{{{ ENTITY MAP



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rbbt/rest/entity/locate.rb', line 302

def locate_entity_map_template_from_resource(resource, type)
  if type == "Default" 
    path = resource.entity_map["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  path = resource.entity_map[type.to_s + ".haml"]
  return path if path.exists?

  nil
end

#locate_entity_template(entity) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbbt/rest/entity/locate.rb', line 57

def locate_entity_template(entity)

  if entity.respond_to? :dir and Path === entity.dir
    entity_views = entity.dir.www.views

    entity.annotation_types.each do |annotation|
      path = entity_views.entity[annotation.to_s + ".haml"]
      return path if path.exists?
    end
  end

  entity_resources.each do |resource|
    path = locate_entity_template_from_resource(resource, entity)
    return path if path and path.exists?
  end

  entity_resources.each do |resource|
    path = locate_entity_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for entity: #{ entity } (#{entity.annotation_types * ", "})"
end

#locate_entity_template_from_resource(resource, entity) ⇒ Object

{{{ ENTITY



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbbt/rest/entity/locate.rb', line 39

def locate_entity_template_from_resource(resource, entity)
  if entity == "Default" 
    path = resource.entity["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  entity.annotation_types.each do |annotation|
    path = resource.entity[annotation.to_s + ".haml"]
    return path if path.exists?
  end

  nil
end

#page_action(path = nil) ⇒ Object



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

def page_action(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity_action", "entity_list_action"
    return Entity::REST.restore_element(path.split("/")[3])
  else
    return nil
  end
end

#page_entity(path = nil) ⇒ Object



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

def page_entity(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity"
    return Entity::REST.restore_element(path.split("/")[3])
  when "entity_action"
    return Entity::REST.restore_element(path.split("/")[4])
  else
    return nil
  end
end

#page_entity_base_type(path = nil) ⇒ Object



152
153
154
# File 'lib/rbbt/rest/entity/helpers.rb', line 152

def page_entity_base_type(path = nil)
  page_entity_type.split(":").first
end

#page_entity_list(path = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbbt/rest/entity/helpers.rb', line 115

def page_entity_list(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity_list"
    return Entity::REST.restore_element(path.split("/")[3])
  when "entity_list_action"
    return Entity::REST.restore_element(path.split("/")[4])
  else
    return nil
  end
end

#page_entity_map(path = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rbbt/rest/entity/helpers.rb', line 128

def page_entity_map(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity_map"
    return Entity::REST.restore_element(path.split("/")[4])
  when "entity_map_action"
    return Entity::REST.restore_element(path.split("/")[5])
  else
    return nil
  end
end

#page_entity_map_column(path = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/rbbt/rest/entity/helpers.rb', line 156

def page_entity_map_column(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when  "entity_map"
    return Entity::REST.restore_element(path.split("/")[3])
  else
    return nil
  end
end

#page_entity_type(path = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/rbbt/rest/entity/helpers.rb', line 141

def page_entity_type(path = nil)
  path = request.path_info if path.nil?

  case page_type
  when "entity", "entity_list", "entity_action", "entity_list_action", "entity_map"
    return Entity::REST.restore_element(path.split("/")[2])
  else
    return nil
  end
end

#page_type(path = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rbbt/rest/entity/helpers.rb', line 70

def page_type(path = nil)
  path = request.path_info if path.nil?

  case
  when path.match(/^\/entity\//)
    return "entity"; 
  when path.match(/^\/entity_action\//)
    return "entity_action"; 
  when path.match(/^\/entity_list\//)
    return "entity_list"; 
  when path.match(/^\/entity_list_action\//)
    return "entity_list_action"; 
  when path.match(/^\/entity_map\//)
    return "entity_map"; 
  when path.match(/^\/entity_map_action\//)
    return "entity_map_action"; 
  else
    return nil;
  end
end

#reject_template(path, binding) ⇒ Object

{{{ CHECKS



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbbt/rest/entity/locate.rb', line 13

def reject_template(path,binding)
  check_file = path.sub(/\.haml$/, '.check')

  if Path === path
    path.annotate check_file 
    return false unless check_file.exists?
  else
    return false unless File.exists?(check_file)
  end

  begin
    code = Open.read(check_file)
    accept = eval code, binding, check_file, 0
    Log.debug{"Checking action template: #{path} - #{accept ? 'accepted' : 'rejected'}"}
    return ! accept
  rescue 
    Log.exception $!
    iii path
    ppp code
    return true
  end
  false
end

#remove_favourite_entity(entity) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbbt/rest/entity/favourites.rb', line 36

def remove_favourite_entity(entity)
  raise "You need to login to have favourites" unless authorized?

  entity_type = entity.base_type
  dir = Path.setup(File.join(settings.favourites_dir, user))

  if (file = dir[entity_type].find).exists?
    entities = Annotated.load_tsv(TSV.open(file))
    entities -= [entity]
    if entities.any?
      Open.write(file, Annotated.tsv(entities.uniq, :all).to_s)
    else
      FileUtils.rm file
    end
  end
end

#remove_favourite_entity_list(entity_type, list) ⇒ Object



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

def remove_favourite_entity_list(entity_type, list)
  raise "You need to login to have favourites" unless authorized?

  dir = Path.setup(File.join(settings.favourite_lists_dir, user))

  if (file = dir[entity_type].find).exists?
    lists = Open.read(file).split("\n")
    lists -= [list]
    if lists.any?
      Open.write(file, lists * "\n")
    else
      FileUtils.rm file
    end
  end
end

#remove_favourite_entity_map(entity_type, column, map) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rbbt/rest/entity/favourites.rb', line 135

def remove_favourite_entity_map(entity_type, column, map)
  raise "You need to login to have favourites" unless authorized?

  column = column.gsub('/', '--')

  dir = Path.setup(File.join(settings.favourite_maps_dir, user))

  if (file = dir[entity_type][column].find).exists?
    maps = Open.read(file).split("\n")
    maps -= [map]
    if maps.any?
      Open.write(file, maps * "\n")
    else
      FileUtils.rm file
    end
  end
end

#setup_entity(type, entity, params) ⇒ Object



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

def setup_entity(type, entity, params)
  base_type, format = type.split ":"
  entity_class = case
                 when Entity.formats.include?(base_type)
                   Entity.formats[base_type] 
                 when Entity.formats.include?(format)
                   Entity.formats[format] 
                 else
                   nil
                 end

  raise "Unknown entity type: #{ type }" if entity_class.nil?

  entity_annotations = {}
  entity_class.annotations.each do |annotation|
    value = consume_parameter annotation, params
    value = Entity::REST.restore_element(value) if String === value
    value = false if value == "false"
    value = true if value == "true"
    entity_annotations[annotation] = value
  end

  entity.extend entity_class
  entity_class.setup_hash(entity, entity_annotations)
  entity.format = format if format and entity.respond_to? :format

  entity
end