class RailsNewsfeed::Activity

Attributes

content[RW]
id[R]
new_record[R]
object[RW]
time[RW]

Public Class Methods

all(opt = {}) click to toggle source

gets all activities

# File lib/rails_newsfeed/activity.rb, line 26
def self.all(opt = {})
  rs = Connection.select(table_name, schema, '*') if opt.empty?
  rs = Connection.select(index_table_name, schema, '*', object: opt[:object]) if opt.key?(:object)
  acts = []
  rs.each { |r| acts.push(create_from_cass(:act, r)) } if rs
  acts
end
create(opt = {}) click to toggle source

creates activity

# File lib/rails_newsfeed/activity.rb, line 35
def self.create(opt = {})
  act = new(opt)
  return nil unless act.save
  act
end
create!(opt = {}) click to toggle source

creates without failling

# File lib/rails_newsfeed/activity.rb, line 42
def self.create!(opt = {})
  create(opt)
rescue
  nil
end
create_from_cass(type, res) click to toggle source

creates from feed cassandra

# File lib/rails_newsfeed/activity.rb, line 88
def self.create_from_cass(type, res)
  h = {}
  temp = type == :act ? '' : 'activity_'
  schema.keys.each do |k|
    key = "#{temp}#{k}"
    val = k == :id || k == :time ? res[key].to_s : res[key]
    h[k] = val
  end
  h[:new_record] = false
  new(h)
end
delete(opt = {}, show_last = true) click to toggle source

deletes activities

# File lib/rails_newsfeed/activity.rb, line 63
def self.delete(opt = {}, show_last = true)
  if opt.key?(:id)
    act = find(opt[:id])
    return true unless act
    return act.delete(show_last)
  elsif opt.key?(:object)
    all(object: opt[:object]).each { |a| a.delete(false) }
    return true
  else
    # truncates activity table and feed tables
    Connection.exec_cql("TRUNCATE #{table_name}")
    Connection.exec_cql("TRUNCATE #{index_table_name}")
    FeedTable.all.each { |r| Connection.exec_cql("TRUNCATE #{r.class.table_name}") }
    return true
  end
end
delete!(opt = {}, show_last = true) click to toggle source

deletes without failling

# File lib/rails_newsfeed/activity.rb, line 81
def self.delete!(opt = {}, show_last = true)
  delete(opt, show_last)
rescue
  false
end
find(id) click to toggle source

finds activity by id

# File lib/rails_newsfeed/activity.rb, line 49
def self.find(id)
  r = Connection.select(table_name, schema, '*', { id: id }, page_size: 1).first
  return nil unless r
  create_from_cass(:act, r)
end
find!(id) click to toggle source

finds without failling

# File lib/rails_newsfeed/activity.rb, line 56
def self.find!(id)
  find(id)
rescue
  nil
end
index_table_name() click to toggle source

gets index table name

# File lib/rails_newsfeed/activity.rb, line 15
def self.index_table_name
  "#{table_name}_index"
end
new(options = {}) click to toggle source

initializes

# File lib/rails_newsfeed/activity.rb, line 101
def initialize(options = {})
  @id = options.key?(:id) ? options[:id] : nil
  @content = options.key?(:content) ? options[:content] : nil
  @object = options.key?(:object) ? options[:object] : nil
  @time = options.key?(:time) ? options[:time] : nil
  @new_record = options.key?(:new_record) ? options[:new_record] : true
end
schema() click to toggle source

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

# File lib/rails_newsfeed/activity.rb, line 21
def self.schema
  { id: :uuid, content: :text, object: :text, time: :timestamp }
end
table_name() click to toggle source

gets table name

# File lib/rails_newsfeed/activity.rb, line 10
def self.table_name
  'activity'
end

Public Instance Methods

delete(show_last = true) click to toggle source

deletes including activities from feed tables

# File lib/rails_newsfeed/activity.rb, line 123
def delete(show_last = true)
  return false if @new_record
  Connection.delete(self.class.table_name, self.class.schema, id: @id)
  Connection.delete(self.class.index_table_name, self.class.schema, object: @object, id: @id) if @object
  return delete_from_feed(@id, nil) unless show_last
  delete_from_feed(@id, last)
end
delete!(show_last = true) click to toggle source

deletes without failling

# File lib/rails_newsfeed/activity.rb, line 132
def delete!(show_last = true)
  delete(show_last)
rescue
  false
end
save() click to toggle source

saves

# File lib/rails_newsfeed/activity.rb, line 110
def save
  return insert if @new_record
  update
end
save!() click to toggle source

saves without failling

# File lib/rails_newsfeed/activity.rb, line 116
def save!
  save
rescue
  false
end
to_h(prefix = nil) click to toggle source

converts to hash

# File lib/rails_newsfeed/activity.rb, line 139
def to_h(prefix = nil)
  { "#{prefix}id".to_sym => @id, "#{prefix}content".to_sym => @content,
    "#{prefix}object".to_sym => @object, "#{prefix}time".to_sym => @time }
end

Protected Instance Methods

delete_from_feed(id, last = nil) click to toggle source

deletes from all feed tables

# File lib/rails_newsfeed/activity.rb, line 174
def delete_from_feed(id, last = nil)
  cqls = []
  FeedTable.all.each do |i|
    i_tbl = i.class.table_name
    i_schema = i.class.schema
    Connection.select(i_tbl, i_schema, '*', { activity_id: id }, filtering: true).each do |r|
      cqls.push(Connection.delete(i_tbl, i_schema, { id: r['id'], activity_id: r['activity_id'].to_s }, true))
      next unless last
      cqls.push(Connection.insert(i_tbl, i_schema, NewsfeedModel.from_cass_act(r['id'], last), true))
    end
  end
  Connection.batch_cqls(cqls.uniq)
  true
end
insert() click to toggle source

inserts

# File lib/rails_newsfeed/activity.rb, line 147
def insert
  return false unless @content
  @id ||= Cassandra::Uuid::Generator.new.now.to_s
  @time ||= Cassandra::Types::Timestamp.new(DateTime.current).to_s
  Connection.insert(self.class.table_name, self.class.schema, to_h)
  Connection.insert(self.class.index_table_name, self.class.schema, to_h) unless @object.nil?
  @new_record = false
  true
end
last() click to toggle source

gets last activity of object

# File lib/rails_newsfeed/activity.rb, line 190
def last
  return nil unless @object
  Connection.select(self.class.index_table_name, self.class.schema, '*', object: @object).first
end
update() click to toggle source

updates

# File lib/rails_newsfeed/activity.rb, line 158
def update
  Connection.update(self.class.table_name, self.class.schema, { id: @id }, to_h)
  # updates from all feed models
  cqls = []
  FeedTable.all.each do |i|
    i_tbl = i.class.table_name
    i_schema = i.class.schema
    Connection.select(i_tbl, i_schema, '*', { activity_id: @id }, filtering: true).each do |r|
      cqls.push(Connection.update(i_tbl, i_schema, { id: r['id'], activity_id: @id }, to_h('activity_'), true))
    end
  end
  Connection.batch_cqls(cqls)
  true
end