Class: RailsNewsfeed::Relation
- Inherits:
-
Object
- Object
- RailsNewsfeed::Relation
- Defined in:
- lib/rails_newsfeed/relation.rb
Class Method Summary collapse
-
.create(from, to, options = {}) ⇒ Object
creates relations between two objects.
-
.delete(from, to, options = {}) ⇒ Object
deletes relations between two objects.
-
.index_table_name ⇒ Object
gets index table name.
-
.related?(from, to) ⇒ Boolean
checks is related.
-
.related_of(from) ⇒ Object
gets relateds of object.
-
.schema ⇒ Object
gets schema DO NOT override this method unless you know what you are doing.
-
.table_name ⇒ Object
gets table name.
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, = {}) 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 .key?(:side) && [: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, = {}) 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 .key?(:side) && [:side] == :both delete(to, from) end |
.index_table_name ⇒ Object
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
55 56 57 58 59 60 61 62 |
# File 'lib/rails_newsfeed/relation.rb', line 55 def self.(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 |
.related_of(from) ⇒ Object
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.(from) = [] 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']) .push(ins) if ins end end |
.schema ⇒ Object
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_name ⇒ Object
gets table name
4 5 6 |
# File 'lib/rails_newsfeed/relation.rb', line 4 def self.table_name 'relation' end |