Class: RailsNewsfeed::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_newsfeed/relation.rb

Class Method Summary collapse

Class Method Details

.create(from, to, options = {}) ⇒ Object

creates relations between two objects



20
21
22
23
24
25
26
27
# File 'lib/rails_newsfeed/relation.rb', line 20

def self.create(from, to, options = {})
  id = Cassandra::Uuid::Generator.new.now.to_s
  record = { id: id, from_class: from.class.name, from_id: from.id, to_class: to.class.name, to_id: to.id }
  return false unless Connection.insert(table_name, schema, record)
  Connection.insert(index_table_name, schema, record)
  return true unless options.key?(:side) && options[:side] == :both
  create(to, from)
end

.delete(from, to, options = {}) ⇒ Object

deletes relations between two objects



30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_newsfeed/relation.rb', line 30

def self.delete(from, to, options = {})
  cond = { from_class: from.class.name, from_id: from.id, to_class: to.class.name, to_id: to.id }
  i = Connection.select(index_table_name, schema, '*', cond).first
  if i
    Connection.delete(table_name, schema, from_class: from.class.name, from_id: from.id, id: i['id'].to_s)
    Connection.delete(index_table_name, schema, cond)
  end
  return true unless options.key?(:side) && options[:side] == :both
  delete(to, from)
end

.index_table_nameObject

gets index table name



9
10
11
# File 'lib/rails_newsfeed/relation.rb', line 9

def self.index_table_name
  "#{table_name}_index"
end

.related?(from, to) ⇒ Boolean

checks is related

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/rails_newsfeed/relation.rb', line 55

def self.related?(from, to)
  cond = { from_class: from.class.name, from_id: from.id, to_class: to.class.name, to_id: to.id }
  i = Connection.select(index_table_name, schema, '*', cond).first
  return false unless i
  cond = { from_class: from.class.name, from_id: from.id, id: i['id'].to_s }
  return false unless Connection.select(table_name, schema, '*', cond).first
  true
end

gets relateds of object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_newsfeed/relation.rb', line 42

def self.related_of(from)
  relateds = []
  result = Connection.select(table_name, schema, '*', from_class: from.class.name, from_id: from.id)
  result.each do |r|
    cons = r['to_class'].safe_constantize
    next unless cons
    ins = cons.new(id: r['to_id'])
    relateds.push(ins) if ins
  end
  relateds
end

.schemaObject

gets schema DO NOT override this method unless you know what you are doing



15
16
17
# File 'lib/rails_newsfeed/relation.rb', line 15

def self.schema
  { id: :uuid, from_class: :text, from_id: :text, to_class: :text, to_id: :text }
end

.table_nameObject

gets table name



4
5
6
# File 'lib/rails_newsfeed/relation.rb', line 4

def self.table_name
  'relation'
end