Class: MontageRails::Relation
- Inherits:
-
Montage::Query
- Object
- Montage::Query
- MontageRails::Relation
- Defined in:
- lib/montage_rails/relation.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#create(params) ⇒ Object
Create a record based on the query relationship.
-
#exists? ⇒ Boolean
Check if a record within the current scope exists.
-
#first ⇒ Object
Just adds a limit of 1 to the query, and forces it to return a singular resource.
-
#initialize(klass) ⇒ Relation
constructor
A new instance of Relation.
-
#inspect ⇒ Object
Utility method to allow viewing of the result set in a console.
-
#loadable? ⇒ Boolean
Checks to see if the relation is loadable If we are in test or dev environment, this is always true, otherwise it falls back to checking the loaded? instance variable.
-
#paginate(page = 1, per_page = 25) ⇒ Object
Support for WillPaginate and Kaminari, if it is defined.
-
#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.
-
#reload ⇒ Object
Force reload of the record.
-
#reset ⇒ Object
Reset the whole shebang.
-
#to_a ⇒ Object
Returns the set of records if they have already been fetched, otherwise gets the records and returns them.
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(schema: klass.table_name) @klass = klass @loaded = false @response = {} end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/montage_rails/relation.rb', line 5 def klass @klass end |
#loaded ⇒ Object (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 |
#response ⇒ Object (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
44 45 46 |
# File 'lib/montage_rails/relation.rb', line 44 def exists? to_a.any? end |
#first ⇒ Object
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 |
#inspect ⇒ Object
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 |
#loadable? ⇒ Boolean
Checks to see if the relation is loadable If we are in test or dev environment, this is always true, otherwise it falls back to checking the loaded? instance variable
73 74 75 |
# File 'lib/montage_rails/relation.rb', line 73 def loadable? %w(test development).include?(Rails.env) || !loaded? end |
#paginate(page = 1, per_page = 25) ⇒ Object
Support for WillPaginate and Kaminari, 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) merge_array(["$pluck", [column_name.to_s]]) map { |r| r.send(column_name.to_sym) } end |
#reload ⇒ Object
Force reload of the record
104 105 106 107 108 |
# File 'lib/montage_rails/relation.rb', line 104 def reload reset to_a self end |
#reset ⇒ Object
Reset the whole shebang
112 113 114 115 116 117 |
# File 'lib/montage_rails/relation.rb', line 112 def reset cache.remove("#{klass}/#{options}") @records = [] @loaded = nil self end |
#to_a ⇒ Object
Returns the set of records if they have already been fetched, otherwise gets the records and returns them
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/montage_rails/relation.rb', line 80 def to_a return @records unless loadable? @response = cache.get_or_set_query(klass, ) do connection.documents({ query: }) end @records = [] if @response.success? records = @response.members.attributes["query"] records.each do |record| @records << klass.new(record.merge(persisted: true)) end @loaded = true end @records end |