Class: RailsNewsfeed::NewsfeedModel

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

Constant Summary collapse

SALT_KEY =
'fQEEumGosuS92VcVVMPm'.freeze
SECRET_KEY =
'sL6PySf7Ijo8L0ZhU7R2'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NewsfeedModel

initializes



49
50
51
52
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 49

def initialize(options = {})
  @id = options.key?(:id) ? options[:id] : nil
  @next_page_token = options.key?(:next_page_token) ? options[:next_page_token] : nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#next_page_tokenObject (readonly)

Returns the value of attribute next_page_token.



10
11
12
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 10

def next_page_token
  @next_page_token
end

Class Method Details

.delete(id, act_id = nil, related = false) ⇒ Object

deletes by id



34
35
36
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 34

def self.delete(id, act_id = nil, related = false)
  new(id: id).delete(act_id, related)
end

.feeds(id, page_size = 10, next_page_token = nil) ⇒ Object

gets feeds by id



39
40
41
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 39

def self.feeds(id, page_size = 10, next_page_token = nil)
  new(id: id, next_page_token: next_page_token).feeds(page_size)
end

.from_cass_act(id, res) ⇒ Object



43
44
45
46
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 43

def self.from_cass_act(id, res)
  { id: id, activity_id: res['id'].to_s, activity_content: res['content'],
    activity_object: res['object'], activity_time: res['time'].to_s }
end

.insert(id, activity, related = true, hide_old = true) ⇒ Object

inserts



29
30
31
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 29

def self.insert(id, activity, related = true, hide_old = true)
  new(id: id).insert(activity, related, hide_old)
end

.schemaObject

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



24
25
26
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 24

def self.schema
  { id: id_type, activity_id: :uuid, activity_content: :text, activity_object: :text, activity_time: :timestamp }
end

.table_nameObject

gets table name



18
19
20
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 18

def self.table_name
  name.demodulize.underscore
end

.type_of_id(type) ⇒ Object

sets type of id



13
14
15
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 13

def self.type_of_id(type)
  self.id_type = type
end

Instance Method Details

#after_feedsObject

overrides this method to implement your own



109
110
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 109

def after_feeds
end

#delete(act_id = nil, related = true) ⇒ Object

deletes an activity or all activities from this feed



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 77

def delete(act_id = nil, related = true)
  tbl = self.class.table_name
  schema = self.class.schema
  if act_id
    return Activity.delete(act_id, true) if related
    return Connection.delete(tbl, schema, id: @id, activity_id: act_id)
  end
  cqls = []
  Connection.select(tbl, schema, '*', id: @id).each do |r|
    if related
      Activity.delete(r['id'].to_s, true)
    else
      cqls.push(Connection.delete(tbl, schema, { id: @id, activity_id: r['id'].to_s }, true))
    end
  end
  Connection.batch_cqls(cqls.uniq)
  true
end

#deregister(to, options = {}) ⇒ Object

deregisters to another



118
119
120
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 118

def deregister(to, options = {})
  Relation.delete(to, self, options)
end

#feeds(page_size = 10) ⇒ Object

gets feeds



97
98
99
100
101
102
103
104
105
106
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 97

def feeds(page_size = 10)
  @feeds = []
  options = { page_size: page_size.to_i }
  options[:paging_state] = decoded_next_page_token if @next_page_token
  result = Connection.select(self.class.table_name, self.class.schema, '*', { id: @id }, options)
  result.each { |r| @feeds.push(Activity.create_from_cass(:feed, r)) }
  encoded_next_page_token(result)
  after_feeds
  @feeds
end

#insert(activity, related = true, hide_old = true) ⇒ Object

inserts an activity into table



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 55

def insert(activity, related = true, hide_old = true)
  record = { id: @id, activity_id: activity.id, activity_content: activity.content,
             activity_object: activity.object, activity_time: activity.time }
  Connection.insert(self.class.table_name, self.class.schema, record)
  ins_arr = []
  cqls = []
  if related
    Relation.related_of(self).each do |ins|
      record[:id] = ins.id
      cqls.push(Connection.insert(ins.class.table_name, ins.class.schema, record, true))
      ins_arr.push(ins) if hide_old
    end
  end
  if hide_old
    ins_arr.push(self)
    cqls |= cql_hide_old_feeds_of(activity, ins_arr)
  end
  Connection.batch_cqls(cqls)
  true
end

#register(to, options = {}) ⇒ Object

registers to another



113
114
115
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 113

def register(to, options = {})
  Relation.create(to, self, options)
end

#register?(to) ⇒ Boolean

checks related

Returns:

  • (Boolean)


123
124
125
# File 'lib/rails_newsfeed/newsfeed_model.rb', line 123

def register?(to)
  Relation.related?(to, self)
end