app/services/linker.rb in rademade_admin-0.0.3 vs app/services/linker.rb in rademade_admin-0.1.0
- old
+ new
@@ -1,34 +1,28 @@
# -*- encoding : utf-8 -*-
module RademadeAdmin
class Linker
- def initialize(model_info, parent_model, parent_id)
+ def initialize(model_info, item, relation)
@model_info = model_info
- @parent_model_info = RademadeAdmin::Model::Graph.instance.model_info(parent_model)
- @parent = @parent_model_info.model.find(parent_id)
+ @item = item
+ @related_data_item = model_info.data_items.data_item(relation)
end
def link(id)
- process_link { |old_data| old_data << parse_id(id) }
+ process_link { |old_data| old_data << @related_data_item.relation.related_entities(id) }
end
def unlink(id)
- process_link { |old_data| old_data - [parse_id(id)] }
+ process_link { |old_data| old_data - [@related_data_item.relation.related_entities(id)] }
end
private
def process_link
- related_name = @model_info.model_name.tableize.gsub('/', '_').to_sym # todo extract method or better guessing of related name
- association = @parent_model_info.reflect_on_association(related_name)
- foreign_key = @parent_model_info.association_foreign_key(association)
- @parent.send(foreign_key + '=', yield(@parent.send(foreign_key)))
- @parent.save
- end
-
- def parse_id(id)
- Integer(id) rescue id.to_s
+ old_data = @item.send(@related_data_item.getter).to_a
+ @item.send(@related_data_item.setter, yield(old_data))
+ @item.save
end
end
end