Class: MontageRails::Relation

Inherits:
Montage::Query
  • Object
show all
Defined in:
lib/montage_rails/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Relation

Returns a new instance of Relation.



14
15
16
17
18
19
# File 'lib/montage_rails/relation.rb', line 14

def initialize(klass)
  super()
  @klass = klass
  @loaded = false
  @response = {}
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/montage_rails/relation.rb', line 5

def klass
  @klass
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



5
6
7
# File 'lib/montage_rails/relation.rb', line 5

def loaded
  @loaded
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/montage_rails/relation.rb', line 5

def response
  @response
end

Instance Method Details

#create(params) ⇒ Object

Create a record based on the query relationship



59
60
61
# File 'lib/montage_rails/relation.rb', line 59

def create(params)
  klass.create(params.merge(query[:filter]))
end

#exists?Boolean

Check if a record within the current scope exists

Returns:

  • (Boolean)


44
45
46
# File 'lib/montage_rails/relation.rb', line 44

def exists?
  to_a.any?
end

#firstObject

Just adds a limit of 1 to the query, and forces it to return a singular resource



38
39
40
# File 'lib/montage_rails/relation.rb', line 38

def first
  limit(1).to_a.first
end

#inspectObject

Utility method to allow viewing of the result set in a console



65
66
67
# File 'lib/montage_rails/relation.rb', line 65

def inspect
  to_a.inspect
end

#paginate(page = 1, per_page = 25) ⇒ Object

Support for WillPaginate, if it is defined. If not, returns self



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/montage_rails/relation.rb', line 23

def paginate(page = 1, per_page = 25)
  if Object.const_defined?("WillPaginate")
    WillPaginate::Collection.create(page, per_page, count) do |pager|
      pager.replace(self[pager.offset, pager.per_page]).to_a
    end
  elsif Object.const_defined?("Kaminari")
    Kaminari.paginate_array(self).page(page).per(per_page)
  else
    self
  end
end

#pluck(column_name) ⇒ Object

Override the pluck method that resides in the Ruby Montage query object To conform with Rails convention, this method should return an array of the result set, not an array of the class instances



52
53
54
55
# File 'lib/montage_rails/relation.rb', line 52

def pluck(column_name)
  query.merge!(pluck: [column_name.to_s])
  map { |r| r.send(column_name.to_sym) }
end

#reloadObject

Force reload of the record



94
95
96
97
98
# File 'lib/montage_rails/relation.rb', line 94

def reload
  reset
  to_a
  self
end

#resetObject

Reset the whole shebang



102
103
104
105
106
107
# File 'lib/montage_rails/relation.rb', line 102

def reset
  cache.remove("#{klass}/#{query}")
  @records = []
  @loaded = nil
  self
end

#to_aObject

Returns the set of records if they have already been fetched, otherwise gets the records and returns them



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/montage_rails/relation.rb', line 72

def to_a
  return @records if loaded?

  @response = cache.get_or_set_query(klass, query) do
    connection.documents(klass.table_name, query)
  end

  @records = []

  if @response.success?
    @response.members.each do |member|
      @records << klass.new(member.attributes.merge(persisted: true))
    end

    @loaded = true
  end

  @records
end