Sha256: 89555d85fae120dd9b1f7e4ab7b2e328fa3b2f830fafbe25cc4bdcfd6186eec4

Contents?: true

Size: 1.99 KB

Versions: 16

Compression:

Stored size: 1.99 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, params={})
        instance.save
      end

      def delete_instance(instance, params={})
        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

16 entries across 16 versions & 1 rubygems

Version Path
trestle-0.10.1 lib/trestle/adapters/sequel_adapter.rb
trestle-0.10.0 lib/trestle/adapters/sequel_adapter.rb
trestle-0.10.0.pre2 lib/trestle/adapters/sequel_adapter.rb
trestle-0.10.0.pre lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.8 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.7 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.6 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.5 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.4 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.3 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.2 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.1 lib/trestle/adapters/sequel_adapter.rb
trestle-0.9.0 lib/trestle/adapters/sequel_adapter.rb
trestle-0.8.13 lib/trestle/adapters/sequel_adapter.rb
trestle-0.8.12 lib/trestle/adapters/sequel_adapter.rb
trestle-0.8.11 lib/trestle/adapters/sequel_adapter.rb