class Pinkman::Serializer::Scope

Attributes

access[RW]
assoc_scopes[RW]
name[RW]
read[RW]
serializer[RW]
write[RW]

Public Class Methods

new(hash) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 7
 def initialize hash
  if hash.is_a?(Hash)
    self.serializer = hash[:serializer]
    self.name = hash[:name]
  end
  self
end

Public Instance Methods

access_actions(*args) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 89
def access_actions *args
  self.access = args
  self.access = [] unless args.first
  access
end
associations() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 40
def associations
  read.select{ |attribute| attribute_is_association?(attribute) }
end
associations_inclusion() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 95
def associations_inclusion
  @associations_inclusion ||= []
end
associations_inclusion=(val) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 99
def associations_inclusion= val
  @associations_inclusion = val
end
attribute_assoc_scope(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 176
def attribute_assoc_scope(attribute)
  assoc_serializer = get_associated_serializer(attribute)
  if assoc_serializer
    assoc_serializer.scope(name.to_sym) 
  else
    binding.pry
    raise ArgumentError, "#{serializer}.#{name}: association named - #{attribute} - found but I can't find its serializer."
  end
end
attribute_has_nested_associated?(attribute) click to toggle source

given that a attribute is a association, and the associated serializer has same scope defined, checks if a nested association is present

# File lib/pinkman/serializer/scope.rb, line 189
def attribute_has_nested_associated?(attribute)
  attribute = attribute.to_s
  if attribute_is_association?(attribute)
    assoc_scope = attribute_assoc_scope(attribute)
    assoc_scope && assoc_scope.associations.any? && assoc_scope.serializer.model != serializer.model
  end
end
attribute_inclusion_clause(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 145
def attribute_inclusion_clause attribute
  if attribute_has_nested_associated?(attribute)
    a = []
    assoc_scope = attribute_assoc_scope(attribute)
    assoc_scope.associations.each do |assoc_attribute|
      a << assoc_scope.attribute_inclusion_clause(assoc_attribute)
    end
    { attribute.to_sym => a }
  else
    attribute.to_sym
  end
end
attribute_is_association?(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 141
def attribute_is_association?(attribute)
  attribute.to_s.in?(reflections.keys.map(&:to_s))
end
attribute_is_in_db?(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 137
def attribute_is_in_db?(attribute)
  attribute.to_s.in?(serializer.model.column_names)
end
can_access() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 127
def can_access
  access
end
can_access?(action) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 115
def can_access? action
  access.include?(:all) or access.include?(action.to_sym)
end
can_read() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 119
def can_read
  read + read_ghost
end
can_read?(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 107
def can_read? attribute
  read.include?(:all) or read.include?(attribute.to_sym) or attribute.to_sym == :error or attribute.to_sym == :errors or read_ghost.include?(attribute.to_sym)
end
can_write() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 123
def can_write
  write
end
can_write?(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 111
def can_write? attribute
  (write.include?(:all) or write.include?(attribute.to_sym)) and (serializer.model.column_names.include?(attribute.to_s) or (serializer.model.instance_methods.include?("#{attribute.to_s}=".to_sym) and write.include?(attribute.to_sym)))
end
fields() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 44
def fields
  read.select{ |attribute| attribute_is_in_db?(attribute) }
end
get_associated_model(reflection) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 160
def get_associated_model(reflection)
  if reflection.options[:polymorphic]
    reflection.klass
  else
    reflection.active_record
  end
end
get_associated_serializer(attribute) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 168
def get_associated_serializer(attribute)
  begin
    get_associated_model(reflections[attribute.to_s]).serializer if attribute_is_association?(attribute.to_s)
  rescue
    raise ArgumentError, "#{serializer}.#{name}: association named - #{attribute} - found but I can't find its serializer."
  end
end
include_associations(*args) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 103
def include_associations *args
  self.associations_inclusion = args
end
include_optimizer() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 62
def include_optimizer
  associations.each do |attribute|
    including << attribute_inclusion_clause(attribute)
  end
  including
end
including() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 73
def including
  @including ||= []
end
query_optimizer() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 48
def query_optimizer 
  select_optimizer
  include_optimizer
end
read_attributes(*args) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 23
def read_attributes *args
  read_attrs = []
  assoc_scopes = {}
  args.each do |field|
    if field.is_a?(Hash)
     assoc_scopes.merge!(field)
     read_attrs = read_attrs + field.keys if field.any?
    else
     read_attrs << field
    end
  end
  self.read = read_attrs
  self.assoc_scopes = assoc_scopes
  query_optimizer
  read
end
read_ghost() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 15
def read_ghost
  @read_ghost || []
end
read_ghost=(value) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 19
def read_ghost= value
  @read_ghost = value
end
read_ghost_attributes(*args) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 77
def read_ghost_attributes *args
  self.read_ghost = args
  self.read_ghost = [] unless args.first
  read_ghost
end
reflections() click to toggle source

private

# File lib/pinkman/serializer/scope.rb, line 133
def reflections
  serializer.reflections
end
select_optimizer() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 53
  def select_optimizer
    unless :all.in?(read.map { |read_attr| begin read_attr.to_sym rescue read_attr end})
      fields.each do |attribute|
        selecting << "#{serializer.table_name}.#{attribute}"
      end
    end
    selecting
  end
  
  def include_optimizer
    associations.each do |attribute|
      including << attribute_inclusion_clause(attribute)
    end
    including
  end
  
  def selecting
    @selecting ||= []
  end
  
  def including
    @including ||= []
  end
  
  def read_ghost_attributes *args
    self.read_ghost = args
    self.read_ghost = [] unless args.first
    read_ghost
  end

  def write_attributes *args
    self.write = args
    self.write = [] unless args.first
    write
  end

  def access_actions *args
    self.access = args
    self.access = [] unless args.first
    access
  end
  
  def associations_inclusion
    @associations_inclusion ||= []
  end
  
  def associations_inclusion= val
    @associations_inclusion = val
  end
  
  def include_associations *args
    self.associations_inclusion = args
  end

  def can_read? attribute
    read.include?(:all) or read.include?(attribute.to_sym) or attribute.to_sym == :error or attribute.to_sym == :errors or read_ghost.include?(attribute.to_sym)
  end

  def can_write? attribute
    (write.include?(:all) or write.include?(attribute.to_sym)) and (serializer.model.column_names.include?(attribute.to_s) or (serializer.model.instance_methods.include?("#{attribute.to_s}=".to_sym) and write.include?(attribute.to_sym)))
  end

  def can_access? action
    access.include?(:all) or access.include?(action.to_sym)
  end

  def can_read
    read + read_ghost
  end

  def can_write
    write
  end

  def can_access
    access
  end
  
  # private
  
  def reflections
    serializer.reflections
  end
  
  def attribute_is_in_db?(attribute)
    attribute.to_s.in?(serializer.model.column_names)
  end
  
  def attribute_is_association?(attribute)
    attribute.to_s.in?(reflections.keys.map(&:to_s))
  end
  
  def attribute_inclusion_clause attribute
    if attribute_has_nested_associated?(attribute)
      a = []
      assoc_scope = attribute_assoc_scope(attribute)
      assoc_scope.associations.each do |assoc_attribute|
        a << assoc_scope.attribute_inclusion_clause(assoc_attribute)
      end
      { attribute.to_sym => a }
    else
      attribute.to_sym
    end
  end
  
  
  
  def get_associated_model(reflection)
    if reflection.options[:polymorphic]
      reflection.klass
    else
      reflection.active_record
    end
  end
  
  def get_associated_serializer(attribute)
    begin
      get_associated_model(reflections[attribute.to_s]).serializer if attribute_is_association?(attribute.to_s)
    rescue
      raise ArgumentError, "#{serializer}.#{name}: association named - #{attribute} - found but I can't find its serializer."
    end
  end
  
  def attribute_assoc_scope(attribute)
    assoc_serializer = get_associated_serializer(attribute)
    if assoc_serializer
      assoc_serializer.scope(name.to_sym) 
    else
      binding.pry
      raise ArgumentError, "#{serializer}.#{name}: association named - #{attribute} - found but I can't find its serializer."
    end
  end

  # given that a attribute is a association,
  # and the associated serializer has same scope defined,
  # checks if a nested association is present
  def attribute_has_nested_associated?(attribute)
    attribute = attribute.to_s
    if attribute_is_association?(attribute)
      assoc_scope = attribute_assoc_scope(attribute)
      assoc_scope && assoc_scope.associations.any? && assoc_scope.serializer.model != serializer.model
    end
  end
  
end
selecting() click to toggle source
# File lib/pinkman/serializer/scope.rb, line 69
def selecting
  @selecting ||= []
end
write_attributes(*args) click to toggle source
# File lib/pinkman/serializer/scope.rb, line 83
def write_attributes *args
  self.write = args
  self.write = [] unless args.first
  write
end