Created
August 21, 2017 13:27
-
-
Save spohlenz/0b218de7a1636f5757bd6d30cc1abbb5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require "sequel" | |
rescue LoadError | |
$stderr.puts "You don't have sequel installed in your application. Please add it to your Gemfile and run bundle install" | |
raise | |
end | |
Sequel::Model.plugin :active_model | |
module Trestle | |
module Adapters | |
module SequelAdapter | |
def collection(params={}) | |
model.dataset | |
end | |
def find_instance(params) | |
model[params[:id]] | |
end | |
def build_instance(attrs={}, params={}) | |
model.new(attrs) | |
end | |
def update_instance(instance, attrs, params={}) | |
instance.set(attrs) | |
end | |
def save_instance(instance) | |
instance.save | |
end | |
def delete_instance(instance) | |
instance.destroy | |
end | |
def unscope(scope) | |
scope.unfiltered | |
end | |
def merge_scopes(scope, other) | |
scope.intersect(other) | |
end | |
def count(collection) | |
collection.count | |
end | |
def sort(collection, field, order) | |
collection.order(Sequel.send(order, field)) | |
end | |
def default_table_attributes | |
default_attributes.reject do |attribute| | |
inheritance_column?(attribute)# || counter_cache_column?(attribute) | |
end | |
end | |
def default_form_attributes | |
default_attributes.reject do |attribute| | |
primary_key?(attribute) || inheritance_column?(attribute)# || counter_cache_column?(attribute) | |
end | |
end | |
protected | |
def default_attributes | |
reflections = model.association_reflections | |
admin.model.db_schema.map do |column_name, column_attrs| | |
if reflection = reflections[column_name] | |
Attribute::Association.new(column_name, class: reflection.class) | |
else | |
Attribute.new(column_name, column_attrs[:type]) | |
end | |
end | |
end | |
def primary_key?(attribute) | |
attribute.name.to_s == model.primary_key.to_s | |
end | |
def inheritance_column?(attribute) | |
model.respond_to?(:sti_key) && attribute.name.to_s == model.sti_key.to_s | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment