Sha256: eedde363bbd127bbd29421ab5731cb3b3c66abe4a0d7aff7e0085de8cb554232

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

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 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)
        end
      end

      def default_form_attributes
        default_attributes.reject do |attribute|
          primary_key?(attribute) || inheritance_column?(attribute)
        end
      end

    protected
      def default_attributes
        model.db_schema.map do |column_name, column_attrs|
          if column_name.to_s.end_with?("_id") && (name = column_name.to_s.sub(/_id$/, '')) && (reflection = model.association_reflection(name.to_sym))
            Attribute::Association.new(column_name, class: -> { reflection.associated_class }, name: name)
          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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trestle-0.8.10 lib/trestle/adapters/sequel_adapter.rb
trestle-0.8.9 lib/trestle/adapters/sequel_adapter.rb
trestle-0.8.8 lib/trestle/adapters/sequel_adapter.rb
trestle-0.8.7 lib/trestle/adapters/sequel_adapter.rb