Sha256: 79f83be0819b410a4315c39da32f7e447d5921b0de06e2d0e304f272d97437fc

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

# extended Relation: extracts relation from rows
class Relation < ActiveRecord::Base

  def self.add(row_from, row_to)
    hsh = normalize(row_from, row_to)
    Relation.create!(hsh)  if Relation.where(hsh).first == nil
  end

  def self.delete(row_from, row_to)
    hsh = normalize(row_from, row_to)
    Relation.where(hsh).delete_all
  end

  def self.references(row, kind)
    klass, name, from_id = normalize2(kind, row)
    name = "#{name} #{klass.name}"
    ids = references_raw(name, from_id)
    klass.where(id: ids)
  end

  def self.followers(kind, row)
    klass, name, to_id = normalize2(kind, row)
    name = "#{klass.name} #{name}"
    ids = followers_raw(name, to_id)
    klass.where(id: ids)
  end

 private
  def self.name_id(resource)
    raise 'missing resource'  unless resource
    [resource.class.name, resource.id]
  end

  def self.normalize(row_from, row_to)
    name_from, from_id = name_id(row_from)
    name_to, to_id     = name_id(row_to)
    name = "#{name_from} #{name_to}"
    { name: name, from_id: from_id, to_id: to_id }
  end

  def self.normalize2(kind, row)
    klass = kind
    klass = kind.constantize  unless klass.kind_of?(Class)
    name, id = name_id(row)
    [klass, name, id]
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
relation-0.3.4 app/models/relation_ext.rb
relation-0.3.3 app/models/relation_ext.rb
relation-0.3.2 app/models/relation_ext.rb
relation-0.3.1 app/models/relation_ext.rb
relation-0.3.0 app/models/relation_ext.rb