app/models/apidae/selection.rb in apidae-0.4.5 vs app/models/apidae/selection.rb in apidae-0.5.0
- old
+ new
@@ -1,20 +1,20 @@
module Apidae
class Selection < ActiveRecord::Base
has_many :apidae_selection_objects, class_name: 'Apidae::SelectionObject', foreign_key: :apidae_selection_id
- has_many :objects, class_name: 'Apidae::Object', source: :apidae_object, through: :apidae_selection_objects
+ has_many :objects, class_name: 'Apidae::Obj', source: :apidae_object, through: :apidae_selection_objects
AGENDA_ENDPOINT = 'agenda/detaille/list-identifiants'
SELECTION_ENDPOINT = 'recherche/list-identifiants'
MAX_COUNT = 100
MAX_LOOPS = 10
validates_presence_of :apidae_id, :reference
before_validation :generate_reference, on: :create
def self.add_or_update(selection_data)
- apidae_sel = Apidae::Selection.where(apidae_id: selection_data[:id]).first_or_initialize
+ apidae_sel = Selection.where(apidae_id: selection_data[:id]).first_or_initialize
apidae_sel.label = selection_data[:nom]
apidae_sel.save!
# Note : should be done with basic collection assignment, but can't make it work...
current_objs = apidae_sel.objects.collect {|obj| obj.apidae_id}
@@ -22,16 +22,16 @@
added = imported_objs - current_objs
removed = current_objs - imported_objs
added.each do |o|
- obj = Apidae::Object.find_by_apidae_id(o)
- Apidae::SelectionObject.create(apidae_selection_id: apidae_sel.id, apidae_object_id: obj.id)
+ obj = Obj.find_by_apidae_id(o)
+ SelectionObject.create(apidae_selection_id: apidae_sel.id, apidae_object_id: obj.id)
end
- removed_ids = Apidae::Object.where(apidae_id: removed).map {|o| o.id}
- Apidae::SelectionObject.where(apidae_selection_id: apidae_sel.id, apidae_object_id: removed_ids).delete_all
+ removed_ids = Obj.where(apidae_id: removed).map {|o| o.id}
+ SelectionObject.where(apidae_selection_id: apidae_sel.id, apidae_object_id: removed_ids).delete_all
end
def results(where_clause, offset, size)
objects.includes(:town).limit(size).offset(offset).where(where_clause)
end
@@ -58,9 +58,13 @@
query_args = build_args(AGENDA_ENDPOINT, {selection_ids: [apidae_id], from: from, to: to})
res = query_api(query_args, true)
$apidae_cache.write(key, res)
end
res
+ end
+
+ def as_text
+ "#{label} (#{apidae_id})"
end
private
def query_api(query_args, all_results = false)