module Trestle::Adapters::ActiveRecordAdapter

Public Instance Methods

build_instance(attrs={}, params={}) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 12
def build_instance(attrs={}, params={})
  model.new(attrs)
end
collection(params={}) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 4
def collection(params={})
  model.all
end
count(collection) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 32
def count(collection)
  collection.count(:all)
end
default_form_attributes() click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 50
def default_form_attributes
  default_attributes.reject do |attribute|
    primary_key?(attribute) || inheritance_column?(attribute) || counter_cache_column?(attribute)
  end
end
default_table_attributes() click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 44
def default_table_attributes
  default_attributes.reject do |attribute|
    inheritance_column?(attribute) || counter_cache_column?(attribute)
  end
end
delete_instance(instance, params={}) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 24
def delete_instance(instance, params={})
  instance.destroy
end
find_instance(params) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 8
def find_instance(params)
  model.find(params[:id])
end
human_attribute_name(attribute, options={}) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 40
def human_attribute_name(attribute, options={})
  model.human_attribute_name(attribute, options)
end
merge_scopes(scope, other) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 28
def merge_scopes(scope, other)
  scope.merge(other)
end
save_instance(instance, params={}) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 20
def save_instance(instance, params={})
  instance.save
end
sort(collection, field, order) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 36
def sort(collection, field, order)
  collection.reorder(field => order)
end
update_instance(instance, attrs, params={}) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 16
def update_instance(instance, attrs, params={})
  instance.assign_attributes(attrs)
end

Protected Instance Methods

array_column?(column) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 81
def array_column?(column)
  column.respond_to?(:array?) && column.array?
end
counter_cache_column?(attribute) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 77
def counter_cache_column?(attribute)
  attribute.name.to_s.end_with?("_count")
end
default_attributes() click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 57
def default_attributes
  model.columns.map do |column|
    if column.name.end_with?("_id") && (name = column.name.sub(/_id$/, '')) && (reflection = model.reflections[name])
      Attribute::Association.new(column.name, class: -> { reflection.klass }, name: name, polymorphic: reflection.polymorphic?, type_column: reflection.foreign_type)
    elsif column.name.end_with?("_type") && (name = column.name.sub(/_type$/, '')) && (reflection = model.reflections[name])
      # Ignore type columns for polymorphic associations
    else
      Attribute.new(column.name, column.type, array_column?(column) ? { array: true } : {})
    end
  end.compact
end
inheritance_column?(attribute) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 73
def inheritance_column?(attribute)
  attribute.name.to_s == model.inheritance_column
end
primary_key?(attribute) click to toggle source
# File lib/trestle/adapters/active_record_adapter.rb, line 69
def primary_key?(attribute)
  attribute.name.to_s == model.primary_key
end