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



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

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

  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]
    input_option ||= {}
    if form_options[:method] != 'POST' && ! input_option.include?(:no_file)
      input_option[:no_file] = true 
      input_options[input] = input_option
    end
  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



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

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



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

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}]"

  Log.low "Rendering #{ entity } #{ action }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action }: #{ entity }".gsub(/\s/,'_'))
end

#entity_card_render(card, &block) ⇒ Object



33
34
35
# File 'lib/rbbt/rest/entity/entity_card.rb', line 33

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



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rbbt/rest/entity/render.rb', line 81

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}]"

  Log.low "Rendering #{list.base_type.to_s} list #{ action }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for list: #{ id }".gsub(/\s/,'_'))
end

#entity_list_card_render(card) ⇒ Object



32
33
34
# File 'lib/rbbt/rest/entity/entity_list_card.rb', line 32

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

#entity_list_render(list, id) ⇒ Object



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

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

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

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

  Log.low "Rendering #{list.base_type.to_s} list: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity list: #{ id }".gsub(/\s/,'_'))
end

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



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbbt/rest/entity/render.rb', line 110

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}]"

  Log.low "Rendering '#{map.key_field} - #{map.fields.first}' map #{ action }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Action #{ action } for map: #{ id }".gsub(/\s/,'_'))
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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbbt/rest/entity/render.rb', line 95

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}]"

  Log.low "Rendering #{type} map: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity map: #{ map_id }".gsub(/\s/,'_'))
end

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



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

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}]"

  Log.low "Rendering #{ entity }: #{ template_file }"
  layout_file = layout ? locate_template("layout") : nil

  render(template_file, locals, layout_file, "Entity: #{ entity }".gsub(/\s/,'_'))
end

#entity_resourcesObject



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

def entity_resources
  EntityRESTHelpers.entity_resources + [Rbbt.share.views.find(:lib)] 
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



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

def find_all_entity_action_templates(entity, check = false)
  resources = resources_for_entity(entity)

  types = entity.annotation_types.collect{|e| e.to_s}
  types << "Default"

  paths = types.inject([]) do |acc,type|
    all_files  = glob_all_server_files(["entity", type, "*.haml"]*"/", resources)
    all_checks = glob_all_server_files(["entity", type, "*.check"]*"/", resources)
    rejected = []
    all_checks.each do |check_file|
      rejected << File.basename(check_file).sub('.check', '') if reject?(check_file, binding)
    end

    accepted = all_files.reject{|path|
      rejected.include? File.basename(path).sub('.haml', '')
    }

    acc += accepted
  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_list_action_templates(list, check = false) ⇒ Object



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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rbbt/rest/entity/locate.rb', line 162

def find_all_entity_list_action_templates(list, check = false)
  resources = resources_for_entity(list)

  types = list.annotation_types.collect{|e| e.to_s}
  types << "Default"

  paths = types.inject([]) do |acc,type|
    all_files  = glob_all_server_files(["entity_list", type, "*.haml"]*"/", resources)
    all_checks = glob_all_server_files(["entity_list", type, "*.check"]*"/", resources)
    rejected = []
    all_checks.each do |check_file|
      rejected << File.basename(check_file).sub('.check', '') if reject?(check_file, binding)
    end

    accepted = all_files.reject{|path|
      rejected.include? File.basename(path).sub('.haml', '')
    }

    acc += accepted
  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_map_action_templates(map, check = false) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
# File 'lib/rbbt/rest/entity/locate.rb', line 246

def find_all_entity_map_action_templates(map, check = false)
  resources = entity_resources

  field = map.key_field

  if map.entity_templates[field] 
    types = map.entity_templates[field].annotation_types
  else
    types = [Entity.formats[field]].compact
  end
  types += ["Default"]

  paths = types.inject([]) do |acc,type|
    all_files  = glob_all_server_files(["entity_map", type, "*.haml"]*"/", resources)
    all_checks = glob_all_server_files(["entity_map", type, "*.check"]*"/", resources)
    rejected = []
    all_checks.each do |check_file|
      rejected << File.basename(check_file).sub('.check', '') if reject?(check_file, binding)
    end

    accepted = all_files.reject{|path|
      rejected.include? File.basename(path).sub('.haml', '')
    }

    acc += accepted
  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
      Log.exception $!
      false
    end
  end if check
  
  actions
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



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

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



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

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



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

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

#locate_entity_action_template(entity, action) ⇒ Object

Raises:



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

def locate_entity_action_template(entity, action)
  resources = resources_for_entity(entity)

  types = entity.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    next if path
    path = locate_server_file(["entity", type, action]*"/", resources, 'haml')
    check_file = locate_server_file(["entity", type, action]*"/", resources, 'check')
    raise "This action was rejected: #{ action }" if reject?(check_file, binding)
  end

  raise TemplateMissing, "Template not found for entity action #{action}: #{ entity } (#{entity.annotation_types * ", "})" if path.nil?

  path
end

#locate_entity_list_action_template(list, action) ⇒ Object

Raises:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rbbt/rest/entity/locate.rb', line 143

def locate_entity_list_action_template(list, action)
  resources = resources_for_entity(list)

  types = list.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    next if path
    path = locate_server_file(["entity_list", type, action]*"/", resources, 'haml')
    check_file = locate_server_file(["entity_list", type, action]*"/", resources, 'check')
    raise "This action was rejected: #{ action }" if reject?(check_file, binding)
  end

  raise TemplateMissing, "Template not found for entity list action #{action} (#{list.annotation_types * ", "})" if path.nil?

  path
end

#locate_entity_list_template(list) ⇒ Object

{{{ ENTITY LIST

Raises:



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

def locate_entity_list_template(list)
  resources = resources_for_entity(list)

  types = list.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    path ||= locate_server_file(["entity_list", type]*"/", resources, 'haml')
  end

  raise TemplateMissing, "Template not found for list (#{list.annotation_types * ", "})" if path.nil?

  path
end

#locate_entity_map_action_template(map, action) ⇒ Object

Raises:



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rbbt/rest/entity/locate.rb', line 221

def locate_entity_map_action_template(map, action)
  resources = entity_resources

  field = map.key_field

  if map.entity_templates[field] 
    types = map.entity_templates[field].annotation_types
  else
    types = [Entity.formats[field]].compact
  end
  types += ["Default"]

  path = nil
  types.each do |type|
    next if path
    path = locate_server_file(["entity_map", type, action]*"/", resources, 'haml')
    check_file = locate_server_file(["entity_map", type, action]*"/", resources, 'check')
    raise "This action was rejected: #{ action }" if reject?(check_file, binding)
  end

  raise TemplateMissing, "Template not found for entity map action #{action} (#{field}--#{map.fields.first})" if path.nil?

  path
end

#locate_entity_map_template(type, column = nil) ⇒ Object

{{{ ENTITY MAP

Raises:



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rbbt/rest/entity/locate.rb', line 205

def locate_entity_map_template(type, column = nil)
  resources = entity_resources

  types = [type.to_s]
  types << "Default"

  path = nil
  types.each do |t|
    path ||= locate_server_file(["entity_map", t]*"/", resources, 'haml')
  end

  raise TemplateMissing, "Template not found for map (#{type}--#{column})" if path.nil?

  path
end

#locate_entity_template(entity) ⇒ Object

{{{ ENTITY

Raises:



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

def locate_entity_template(entity)
  resources = resources_for_entity(entity)

  types = entity.annotation_types.collect{|e| e.to_s}
  types << "Default"

  path = nil
  types.each do |type|
    path ||= locate_server_file(["entity", type]*"/", resources, 'haml')
  end

  raise TemplateMissing, "Template not found for entity: #{ entity } (#{entity.annotation_types * ", "})" if path.nil?

  path
end

#page_action(path = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/rbbt/rest/entity/helpers.rb', line 110

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbbt/rest/entity/helpers.rb', line 97

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



158
159
160
# File 'lib/rbbt/rest/entity/helpers.rb', line 158

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

#page_entity_list(path = nil) ⇒ Object



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

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



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rbbt/rest/entity/helpers.rb', line 134

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



162
163
164
165
166
167
168
169
170
171
# File 'lib/rbbt/rest/entity/helpers.rb', line 162

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



147
148
149
150
151
152
153
154
155
156
# File 'lib/rbbt/rest/entity/helpers.rb', line 147

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rbbt/rest/entity/helpers.rb', line 76

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?(check_file, binding) ⇒ Boolean

{{{ CHECKS

Returns:

  • (Boolean)


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

def reject?(check_file, binding)
  return false if check_file.nil? or not check_file.exists?
  begin
    code = Open.read(check_file)
    accept = eval code, binding, check_file, 0
    action = File.basename(check_file).sub('.check', '')
    Log.debug{"Checking action template: #{action} - #{accept ? 'accepted' : 'rejected'}"}
    return ! accept
  rescue 
    Log.exception $!
    return true
  end
  false
end

#reject_template(path, binding) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rbbt/rest/entity/locate.rb', line 34

def reject_template(path, binding)
  return false if path.nil?
  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

  reject?(check_file, binding)
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

#resources_for_entity(entity) ⇒ Object



11
12
13
14
15
# File 'lib/rbbt/rest/entity/locate.rb', line 11

def resources_for_entity(entity)
  resources = entity_resources
  resources.unshift entity.dir.www.views if entity.respond_to? :dir and Path === entity.dir
  resources
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
35
36
# 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?

  raise "Entity type not supported in REST: #{ type }" unless entity_class.include? Entity::REST

  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