module RemoteRelation

Public Class Methods

new(xmodel, params={}) click to toggle source
# File lib/nube/remote_relation.rb, line 14
def initialize(xmodel, params={})
  @params = params
  @xmodel = xmodel
end

Public Instance Methods

add_reflection(type, rel, opts) click to toggle source
# File lib/nube/remote_relation.rb, line 193
def add_reflection(type, rel, opts)
  return if opts.has_key?(:foreign_key) or opts.has_key?(:polymorphic)
  klass = opts.has_key?(:class_name) ? opts[:class_name].constantize : rel.to_s.singularize.camelize.constantize
  unless opts.has_key?(:through)
    foreign_key = if opts.has_key?(:foreign_key)
                    opts[:foreign_key]
                  else
                    (type == :belongs_to) ? "#{rel.to_s.singularize}_id" : "#{self.to_s.singularize.underscore}_id"
                  end
    (@@reflections[self] ||= []) << {type: type, rel: rel, klass: klass, foreign_key: foreign_key}
  end
end
all() click to toggle source
# File lib/nube/remote_relation.rb, line 49
def all
  @xmodel.get(@params.delete(:site_options), @params).map{|attributes| @xmodel.new(attributes, false) }
end
calculate_offset() click to toggle source
# File lib/nube/remote_relation.rb, line 100
def calculate_offset; offset(@params[:limit] * (@page - 1)); end
check_relation(relation) click to toggle source

Need implementation in whole relations

# File lib/nube/remote_relation.rb, line 137
def check_relation(relation)
  (relation - reflection.map{|r| r[:rel] }).empty?
end
controller_name() click to toggle source
# File lib/nube/remote_relation.rb, line 25
def controller_name
  @xmodel.name.pluralize.underscore
end
count() click to toggle source
# File lib/nube/remote_relation.rb, line 45
def count
  @xmodel.get(@params.delete(:site_options).merge({ action: 'count' }), @params).first.to_i
end
create(attrs={}) click to toggle source
# File lib/nube/remote_relation.rb, line 74
def create(attrs={})
  obj = @xmodel.new(attrs)
  obj.save
  obj
end
destroy_all(where={}, relations={}) click to toggle source
# File lib/nube/remote_relation.rb, line 29
def destroy_all(where={}, relations={})
  @xmodel.delete(@params.delete(:site_options).merge({ action: 'destroy_all' }), { where: [where].flatten }).map{|attrs| @xmodel.new(attrs, false) }
end
empty_attributes() click to toggle source
# File lib/nube/remote_relation.rb, line 41
def empty_attributes
  @xmodel.get(@params.delete(:site_options).merge({ action: 'empty_attributes' })).first
end
find(*ids) click to toggle source
# File lib/nube/remote_relation.rb, line 19
def find(*ids)
  @params.merge!({ where: [{id: ids.flatten}] })
  resources = all
  (resources.size > 1) ? resources : resources.first
end
first() click to toggle source
# File lib/nube/remote_relation.rb, line 53
def first
  (@params[:order] ||= []) << { id: :asc }
  @params[:limit] = 1
  all.first
end
foreign_key(rel) click to toggle source
# File lib/nube/remote_relation.rb, line 209
def foreign_key(rel)
  reflection(rel)[:foreign_key]
end
joins(*models) click to toggle source
# File lib/nube/remote_relation.rb, line 65
def joins(*models)
  if @xmodel.same_class?(models)
    params[:joins]= [] unless params.has_key?(:joins)
    tap{ |s| s.params[:joins] += models }
  else
    raise 'Errors: Some class is not Remote Resource'
  end
end
last() click to toggle source
# File lib/nube/remote_relation.rb, line 59
def last
  (@params[:order] ||= []) << { id: :desc }
  @params[:limit] = 1
  all.last
end
limit(number) click to toggle source
# File lib/nube/remote_relation.rb, line 84
def limit(number); tap{|s| s.params[:limit] = number }; end
massive_creation(transaction) click to toggle source
# File lib/nube/remote_relation.rb, line 115
def massive_creation(transaction); massive_transactions(transaction, 'post', 'massive_creation', site_options); end
massive_sum(transaction) click to toggle source
# File lib/nube/remote_relation.rb, line 117
def massive_sum(transaction); massive_transactions(transaction, 'put', 'massive_sum', site_options); end
massive_transactions(transaction, method, action) click to toggle source
# File lib/nube/remote_relation.rb, line 111
def massive_transactions(transaction, method, action)
  @xmodel.send(method, @params.delete(:site_options).merge({ action: action }), @params.merge(transaction: transaction))
end
massive_update(transaction) click to toggle source
# File lib/nube/remote_relation.rb, line 119
def massive_update(transaction); massive_transactions(transaction, 'put', 'massive_update', site_options); end
method_missing(name, *args, &block) click to toggle source
# File lib/nube/remote_relation.rb, line 121
def method_missing(name, *args, &block)
  all.send(name, *args, &block)
end
offset(number) click to toggle source
# File lib/nube/remote_relation.rb, line 86
def offset(number); tap{|s| s.params[:offset] = number }; end
order(conditions) click to toggle source
# File lib/nube/remote_relation.rb, line 82
def order(conditions); tap{|s| (s.params[:order] || s.params[:order] = []) << conditions}; end
page(size=1) click to toggle source
# File lib/nube/remote_relation.rb, line 88
def page(size=1)
  @page = size
  limit(25) unless @params.has_key?(:limit)
  calculate_offset
end
paginate(value={}) click to toggle source
# File lib/nube/remote_relation.rb, line 102
def paginate(value={})
  value.has_key?(:page)     ? page(value[:page])    : page
  value.has_key?(:per_page) ? per(value[:per_page]) : per
end
per(size=25) click to toggle source
# File lib/nube/remote_relation.rb, line 94
def per(size=25)
  @page ||= 1
  limit(size)
  calculate_offset
end
polymorphic_belongs_to(rel, opts={}) click to toggle source
# File lib/nube/remote_relation.rb, line 218
def polymorphic_belongs_to(rel, opts={})
  self.send("#{rel}_type").constantize.find(self.send("#{rel}_id"))
end
polymorphic_has_many(rel, opts={}) click to toggle source
# File lib/nube/remote_relation.rb, line 226
def polymorphic_has_many(rel, opts={})
  self.class.xclass(rel).where("#{opts[:as]}_id" => self.id, "#{opts[:as]}_type" => self.class.to_s)
end
polymorphic_has_one(rel, opts={}) click to toggle source
# File lib/nube/remote_relation.rb, line 222
def polymorphic_has_one(rel, opts={})
  polymorphic_has_many(rel, opts={}).first
end
remote_belongs_to(rel, opts={}) click to toggle source
# File lib/nube/remote_relation.rb, line 165
def remote_belongs_to(rel, opts={})
  add_reflection(:belongs_to, rel, opts)

  self.class_eval do
    define_method rel do
      return polymorphic_belongs_to(rel, opts) if opts.has_key?(:polymorphic)
      class_rel = self.class.xclass(rel)
      foreign_key = self.class.foreign_key(rel)
      send(foreign_key).nil? ? nil : class_rel.find(send(foreign_key))
    end
  end
end
remote_has_many(rel, opts={}) click to toggle source
# File lib/nube/remote_relation.rb, line 141
def remote_has_many(rel, opts={})
  add_reflection(:has_many, rel, opts)
  self.class_eval do
    define_method rel do
      return polymorphic_has_many(rel, opts) if opts.has_key?(:as)
      if opts.has_key?(:through)
        through_class = opts[:through].to_s.singularize.camelize.constantize
        klass = through_class.xclass(rel)
        through_obj = send(opts[:through])
        through_rel = through_class.reflection(rel)

        collect_through = (through_rel[:type] == :belong_to) ? through_rel[:foreign_key] : "id"
        foreign_key     = (through_rel[:type] == :belong_to) ? "id"                      : through_rel[:foreign_key]

        ids = through_obj.is_a?(through_class) ? [through_obj.send(collect_through)] : through_obj.all.collect(&"{collect_through}".to_sym)

        klass.where(foreign_key => ids)
      else
        self.class.xclass(rel).where(self.class.foreign_key(rel) => self.id)
      end
    end
  end
end
remote_has_one(rel, opts={}) click to toggle source
# File lib/nube/remote_relation.rb, line 178
def remote_has_one(rel, opts={})
  add_reflection(:has_one, rel, opts)
  self.class_eval do
    define_method rel do
      return polymorphic_has_one(rel, opts) if opts.has_key?(:as)

      if opts.has_key?(:through)
        send(opts[:through]).send(rel)
      else
        self.class.xclass(rel).where(self.class.foreign_key(rel) => self.id).first
      end
    end
  end
end
remote_reflections() click to toggle source
# File lib/nube/remote_relation.rb, line 206
def remote_reflections; @@reflections[self]; end
same_class?(*relations) click to toggle source
# File lib/nube/remote_relation.rb, line 132
def same_class?(*relations)
  reflection.select{|r| relations.include?(r[:rel]) }.collect{|r| r[:klass] }.map{|k| k.superclass == self.superclass }.all?
end
update_all(changes={}, where={}, relations={}) click to toggle source
# File lib/nube/remote_relation.rb, line 33
def update_all(changes={}, where={}, relations={})
  changes.each do |attrib, value|
    # change true and false by 1 or 0 becouse params are sent as string
    changes[attrib] = (value ? 1 : 0) if !!value == value
  end
  @xmodel.put(@params.delete(:site_options).merge({ action: 'update_all' }), { changes: changes, where: [where].flatten }).first
end
where(conditions) click to toggle source
# File lib/nube/remote_relation.rb, line 80
def where(conditions); tap{|s| (s.params[:where] || s.params[:where] = []) << conditions}; end
xclass(rel) click to toggle source
# File lib/nube/remote_relation.rb, line 213
def xclass(rel)
  reflection(rel)[:klass]
end