Sha256: feff3902213a3c891032f5ac33f2c3acce58bfc9195641ec2ff942793a8f2b9b

Contents?: true

Size: 691 Bytes

Versions: 4

Compression:

Stored size: 691 Bytes

Contents

# -*- encoding : utf-8 -*-
module RademadeAdmin
  class Linker

    def initialize(model_info, item, relation)
      @model_info = model_info
      @item = item
      @related_data_item = model_info.data_items.data_item(relation)
    end

    def link(id)
      process_link { |old_data| old_data << @related_data_item.relation.related_entities(id) }
    end

    def unlink(id)
      process_link { |old_data|
        old_data.delete_if { |data_item| data_item.id.to_s == id.to_s }
      }
    end

    private

    def process_link
      old_data = @item.send(@related_data_item.getter).to_a
      @item.send(@related_data_item.setter, yield(old_data))
      @item.save
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rademade_admin-0.2.2 app/services/linker.rb
rademade_admin-0.2.1 app/services/linker.rb
rademade_admin-0.2.0 app/services/linker.rb
rademade_admin-0.1.6 app/services/linker.rb