class NinjaModel::Base

Public Class Methods

build_finder_relation(options = {}, scope = nil) click to toggle source
# File lib/ninja_model/base.rb, line 72
def build_finder_relation(options = {}, scope = nil)
  relation = options.is_a?(Hash) ? unscoped.apply_finder_options(options) : options
  relation = scope.merge(relation) if scope
  relation
end
compute_type(type_name) click to toggle source
# File lib/ninja_model/base.rb, line 78
def compute_type(type_name)
  if type_name.match(/^::/)
    # If the type is prefixed with a scope operator then we assume that
    # the type_name is an absolute reference.
    ActiveSupport::Dependencies.constantize(type_name)
  else
    # Build a list of candidates to search for
    candidates = []
    name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
    candidates << type_name

    candidates.each do |candidate|
      begin
        constant = ActiveSupport::Dependencies.constantize(candidate)
        return constant if candidate == constant.to_s
      rescue NameError => e
        # We don't want to swallow NoMethodError < NameError errors
        raise e unless e.instance_of?(NameError)
      end
    end

    raise NameError, "uninitialized constant #{candidates.first}"
  end
end
current_scope() click to toggle source
# File lib/ninja_model/base.rb, line 58
def current_scope
  current_scoped_methods
end
current_scoped_methods() click to toggle source
# File lib/ninja_model/base.rb, line 62
def current_scoped_methods
  last = scoped_methods.last
  last.is_a?(Proc) ? unscoped(&last) : last
end
default_scope(scope = {}) click to toggle source
# File lib/ninja_model/base.rb, line 53
def default_scope(scope = {})
  scope = Proc.new if block_given?
  self.default_scopes = default_scopes + [scope]
end
logger() click to toggle source
# File lib/ninja_model/base.rb, line 40
def logger
  ::NinjaModel.logger
end
new(attributes = nil, options = {}) { |self| ... } click to toggle source
# File lib/ninja_model/base.rb, line 124
def initialize(attributes = nil, options = {})
  @attributes = attributes_from_model_attributes
  @association_cache = {}
  @aggregation_cache = {}
  @persisted = false
  @readonly = true
  @destroyed = false

  populate_with_current_scope_attributes

  self.attributes = attributes unless attributes.nil?

  yield self if block_given?
  run_callbacks :initialize
end
relation() click to toggle source
# File lib/ninja_model/base.rb, line 36
def relation
  @relation ||= Relation.new(self)
end
reset_scoped_methods() click to toggle source
# File lib/ninja_model/base.rb, line 67
def reset_scoped_methods
  Thread.current["#{self}_scoped_methods".to_sym] = nil
end
scoped_methods() click to toggle source
# File lib/ninja_model/base.rb, line 48
def scoped_methods
  key = "#{self}_scoped_methods".to_sym
  Thread.current[key] = Thread.current[key].presence || self.default_scopes.dup
end
unscoped() { || ... } click to toggle source
# File lib/ninja_model/base.rb, line 44
def unscoped
  block_given? ? relation.scoping { yield } : relation
end

Public Instance Methods

assign_attributes(new_attributes, options = {}) click to toggle source
# File lib/ninja_model/base.rb, line 104
def assign_attributes(new_attributes, options = {})
  return unless new_attributes

  attributes = new_attributes.stringify_keys

  attributes.each do |k, v|
    if respond_to?("#{k}=")
      send("#{k}=", v)
    else
      raise(StandardError, "unknown attribute: #{k}")
    end
  end
end
attribute_for_inspect(attr_name) click to toggle source
# File lib/ninja_model/base.rb, line 156
def attribute_for_inspect(attr_name)
  value = read_attribute(attr_name)
  if value.is_a?(String) && value.length > 50
    "#{value[0..50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_s(:db)}")
  else
    value.inspect
  end
end
attributes() click to toggle source
# File lib/ninja_model/base.rb, line 118
def attributes
  self.class.attribute_names.inject({}) { |h, v|
    h[v] = read_attribute(v); h
  }
end
derive_class(association_id) click to toggle source
# File lib/ninja_model/base.rb, line 150
def derive_class(association_id)
  klass = association_id.to_s.camelize
  klass = klass.singularize
  compute_type(klass)
end
inspect() click to toggle source
# File lib/ninja_model/base.rb, line 167
def inspect
  attributes_as_nice_string = self.class.attribute_names.collect { |attr|
    "#{attr}: #{attribute_for_inspect(attr)}"
  }.compact.join(', ')
  "#<#{self.class} #{attributes_as_nice_string}>"
end
instantiate(record) click to toggle source
# File lib/ninja_model/base.rb, line 140
def instantiate(record)
  @attributes = record.stringify_keys
  @readonly = @destroyed = false
  @persisted = true

  _run_find_callbacks
  _run_initialize_callbacks
  self
end

Private Instance Methods

populate_with_current_scope_attributes() click to toggle source
# File lib/ninja_model/base.rb, line 177
def populate_with_current_scope_attributes
  return unless self.class.scope_attributes?

  self.class.scope_attributes.each do |att, value|
    send("#{att}=", value) if respond_to?("#{att}=")
  end
end