Sha256: 5d68ed43eb90726164347f32a7def7d487e4da1a33d5d74fde3120b03dc45d2d

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

module BlacklightUserGeneratedContent::ActiveRecordDuckType

  def self.included base
    # ActiveModel/ActiveRecord stuff
    base.send :include, ActiveModel::Observing
    base.send :extend, ActiveModel::Naming
    base.send :extend, ActiveModel::Callbacks
    base.send :extend, ActiveModel::Observing
    base.send :define_model_callbacks, :destroy, :save
    base.send :include, ActiveRecord::Associations
    base.send :include, ActiveRecord::Reflection

    # Mock ActiveRecord methods only to the extent required
    base.send :extend, ActiveRecordClassMethods
    base.send :include, ActiveRecordInstanceMethods
  end

  # ActiveRecord mock methods
  module ActiveRecordClassMethods
    def base_class
      self
    end

    def primary_key
      :id
    end
    
    def compute_type(type_name)
      ActiveSupport::Dependencies.constantize(type_name)
    end
    
    def quote_value *args
      ActiveRecord::Base.quote_value *args
    end
    
    def table_exists?
       false
    end
  end

  
  # ActiveRecord instance mock methods
  module ActiveRecordInstanceMethods
    def quoted_id
      ActiveRecord::Base.quote_value id
    end
  
    def interpolate_sanitized_sql *args
      active_record_model.send :interpolate_sanitized_sql, *args
    end
    def interpolate_and_sanitize_sql *args
      # XXX interpolate_and_sanitize_sql is an ActiveRecord::Base method,
      # XXX rather than create a mock object or something, just use a model we know a priori exists
      active_record_model.send  :interpolate_and_sanitize_sql, *args
    end
  
    def new_record?
      false
    end
  
    def destroyed?
      false
    end

    def persisted?
      false
    end

    private
    def active_record_model
      ActsAsTaggableOn::Tag.new
    end
  end

  def save
    _run_save_callbacks do
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
blacklight_user_generated_content-0.0.6 lib/blacklight_user_generated_content/active_record_duck_type.rb
blacklight_user_generated_content-0.0.5 lib/blacklight_user_generated_content/active_record_duck_type.rb
blacklight_user_generated_content-0.0.4 lib/blacklight_user_generated_content/active_record_duck_type.rb
blacklight_user_generated_content-0.0.3 lib/blacklight_user_generated_content/active_record_duck_type.rb
blacklight_user_generated_content-0.0.2 lib/blacklight_user_generated_content/active_record_duck_type.rb
blacklight_user_generated_content-0.0.1 lib/blacklight_user_generated_content/active_record_duck_type.rb