Created
August 6, 2017 18:13
-
-
Save okliv/52465d38a116688edf0033985e273d27 to your computer and use it in GitHub Desktop.
Adding Sequel adapter to Trestle Admin
This file contains hidden or 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
Index: lib/trestle/adapters.rb | |
autoload :ActiveRecordAdapter | |
autoload :DraperAdapter | |
+ autoload :SequelAdapter | |
class Adapter | |
attr_reader :admin | |
Index: lib/trestle/attribute.rb | |
end | |
def primary_key? | |
- name.to_s == admin.model.primary_key | |
+ name.to_s == admin.model.primary_key.to_s | |
end | |
def inheritance_column? | |
- name.to_s == admin.model.inheritance_column | |
+ name.to_s == admin.model.try(:inheritance_column) | |
end | |
def counter_cache? | |
Index: lib/trestle/configuration.rb | |
option :persistent_params, [:sort, :order, :scope] | |
- option :default_adapter, Adapters::Adapter.compose(Adapters::ActiveRecordAdapter, Adapters::DraperAdapter) | |
+ option :default_adapter, Adapters::Adapter.compose(Adapters::ActiveRecordAdapter, Adapters::DraperAdapter, Adapters::SequelAdapter) | |
option :root_breadcrumbs, -> { [Trestle::Breadcrumb.new(I18n.t("admin.breadcrumbs.home", default: "Home"), Trestle.config.path)] } | |
Index: lib/trestle/form/field.rb | |
end | |
def errors | |
- errors = builder.object.errors[name] | |
- errors += builder.object.errors[name.to_s.sub(/_id$/, '')] if name.to_s =~ /_id$/ | |
+ errors = builder.object.errors[name].to_a | |
+ errors += builder.object.errors[name.to_s.sub(/_id$/, '')].to_a if name.to_s =~ /_id$/ | |
errors | |
end | |
Index: lib/trestle/adapters/sequel_adapter.rb | |
+Sequel::Model.plugin :active_model | |
+ | |
+module Trestle | |
+ module Adapters | |
+ module SequelAdapter | |
+ def collection(params={}) | |
+ admin.model.dataset | |
+ end | |
+ | |
+ def find_instance(params) | |
+ admin.model[params[:id]] | |
+ end | |
+ | |
+ def build_instance(params={}) | |
+ admin.model.new(params) | |
+ end | |
+ | |
+ def update_instance(instance, params) | |
+ instance.set(params) | |
+ end | |
+ | |
+ def save_instance(instance) | |
+ instance.save | |
+ end | |
+ | |
+ def delete_instance(instance) | |
+ instance.destroy | |
+ end | |
+ | |
+ def to_param(instance) | |
+ instance | |
+ end | |
+ | |
+ def unscope(scope) | |
+ scope.unfiltered | |
+ end | |
+ | |
+ def merge_scopes(scope, other) | |
+ scope.where(id: other.select(:id)) | |
+ end | |
+ | |
+ def sort(collection, field, order) | |
+ collection.order(Sequel.send(order, field)) | |
+ end | |
+ | |
+ def paginate(collection, params) | |
+ collection = Kaminari.paginate_array(collection.to_a) unless collection.respond_to?(:page) | |
+ collection.page(params[:page]) | |
+ end | |
+ | |
+ def count(collection) | |
+ collection.count | |
+ end | |
+ | |
+ def default_attributes | |
+ reflections = admin.model.association_reflections | |
+ admin.model.db_schema.map do |column_name, column_attrs| | |
+ if reflection = reflections[column_name] | |
+ Attribute::Association.new(admin, column_name, reflection.class.safe_constantize) | |
+ else | |
+ Attribute.new(admin, column_name, column_attrs[:type]) | |
+ end | |
+ end | |
+ end | |
+ end | |
+ end | |
+end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment