Module: Mongoid::Timeline::Record::ClassMethods

Defined in:
lib/obscured-timeline/record.rb

Instance Method Summary collapse

Instance Method Details

#by(params = {}, options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/obscured-timeline/record.rb', line 52

def by(params = {}, options = {})
  limit = options[:limit].blank? ? nil : options[:limit].to_i
  skip = options[:skip].blank? ? nil : options[:skip].to_i
  order = options[:order].blank? ? :created_at.desc : options[:order]
  only = options[:only].blank? ? %i[id type message producer created_at updated_at proprietor] : options[:only]

  query = {}
  query[:type] = params[:type].to_sym if params[:type]
  query[:severity] = params[:severity].to_sym if params[:severity]
  query[:producer] = params[:producer].to_sym if params[:producer]
  params[:proprietor]&.map { |k, v| query.merge!("proprietor.#{k}" => v) }

  criterion = where(query).only(only).limit(limit).skip(skip)
  criterion = criterion.order_by(order) if order

  docs = criterion.to_a
  docs
end

#make(params = {}) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/obscured-timeline/record.rb', line 30

def make(params = {})
  raise ArgumentError, 'type missing' if params[:type].blank?
  raise ArgumentError, 'type must be a symbol' unless params[:type].instance_of?(Symbol)
  raise ArgumentError, 'message missing' if params[:message].nil?
  raise ArgumentError, 'producer missing' if params[:producer].blank?
  raise ArgumentError, 'proprietor missing' if params[:proprietor].blank?

  doc = new
  doc.type = params[:type]
  doc.severity = params[:severity].to_sym unless params[:severity].blank?
  doc.message = params[:message]
  doc.producer = params[:producer]
  doc.proprietor = params[:proprietor]
  doc
end

#make!(params = {}) ⇒ Object



46
47
48
49
50
# File 'lib/obscured-timeline/record.rb', line 46

def make!(params = {})
  doc = make(params)
  doc.save!
  doc
end