Sha256: 9ccc94b879fcfc59f497fee5cf1b8ad34013a46a2dadf371d01cf8bf1ffccb09

Contents?: true

Size: 1.36 KB

Versions: 12

Compression:

Stored size: 1.36 KB

Contents

class ModelStub < ActiveRecord::Base
  validates :b, :presence => true
  has_one :other_model, :class_name => 'ModelStub'
  has_many :other_models, :class_name => 'ModelStub'

  cattr_accessor :stubbed_columns
  self.stubbed_columns = [:a, :b, :c, :d, :id, :created_at]
  attr_accessor *stubbed_columns
  self.primary_key = :id

  @@nested_scope_calls = []
  cattr_accessor :nested_scope_calls

  scope :a_is_defined, -> { where.not(:a => nil) }
  scope :b_like, ->(pattern) { where('b like ?', pattern) }

  def self.a_is_defined
    @@nested_scope_calls << :a_is_defined
    self
  end

  def self.b_like(pattern)
    @@nested_scope_calls << :b_like
    self
  end

  attr_writer :other_model
  def other_model
    @other_model || nil
  end

  attr_writer :other_models
  def other_models
    @other_models || []
  end

  def self.columns
    @columns ||= stubbed_columns.map do |c|
      column = ColumnMock.new(c.to_s, '', 'varchar(255)')
      column.primary = true if c.to_s == self.primary_key.to_s && column.respond_to?(:primary=)
      column
    end
  end

  def self.columns_hash
    @columns_hash ||= columns.each_with_object({}) { |column, hash| hash[column.name.to_s] = column }
  end

  # column-level security methods, used for testing
  def self.a_authorized_for_bar?
    true
  end
  def self.b_authorized?
    false
  end
  def self.c_authorized_for_create?
    false
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
active_scaffold-3.4.43 test/model_stub.rb
active_scaffold-3.4.42 test/model_stub.rb
active_scaffold-3.4.41.1 test/model_stub.rb
active_scaffold-3.4.41 test/model_stub.rb
active_scaffold-3.4.40 test/model_stub.rb
active_scaffold-3.4.39 test/model_stub.rb
active_scaffold-3.4.38 test/model_stub.rb
active_scaffold-3.4.37 test/model_stub.rb
active_scaffold-3.4.36 test/model_stub.rb
active_scaffold-3.4.35 test/model_stub.rb
active_scaffold-3.4.34 test/model_stub.rb
active_scaffold-3.4.33 test/model_stub.rb