File: active_record.rb

package info (click to toggle)
ruby-orm-adapter 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 180 kB
  • ctags: 88
  • sloc: ruby: 698; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,344 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'active_record'

module OrmAdapter
  class ActiveRecord < Base
    # Return list of column/property names
    def column_names
      klass.column_names
    end

    # @see OrmAdapter::Base#get!
    def get!(id)
      klass.find(wrap_key(id))
    end

    # @see OrmAdapter::Base#get
    def get(id)
      klass.where(klass.primary_key => wrap_key(id)).first
    end

    # @see OrmAdapter::Base#find_first
    def find_first(options = {})
      construct_relation(klass, options).first
    end

    # @see OrmAdapter::Base#find_all
    def find_all(options = {})
      construct_relation(klass, options)
    end

    # @see OrmAdapter::Base#create!
    def create!(attributes = {})
      klass.create!(attributes)
    end

    # @see OrmAdapter::Base#destroy
    def destroy(object)
      object.destroy && true if valid_object?(object)
    end

  protected
    def construct_relation(relation, options)
      conditions, order, limit, offset = extract_conditions!(options)

      relation = relation.where(conditions_to_fields(conditions))
      relation = relation.order(order_clause(order)) if order.any?
      relation = relation.limit(limit) if limit
      relation = relation.offset(offset) if offset

      relation
    end

    # Introspects the klass to convert and objects in conditions into foreign key and type fields
    def conditions_to_fields(conditions)
      fields = {}
      conditions.each do |key, value|
        if value.is_a?(::ActiveRecord::Base) && (assoc = klass.reflect_on_association(key.to_sym)) && assoc.belongs_to?

          if ::ActiveRecord::VERSION::STRING < "3.1"
            fields[assoc.primary_key_name] = value.send(value.class.primary_key)
            fields[assoc.options[:foreign_type]] = value.class.base_class.name.to_s if assoc.options[:polymorphic]
          else # >= 3.1
            fields[assoc.foreign_key] = value.send(value.class.primary_key)
            fields[assoc.foreign_type] = value.class.base_class.name.to_s if assoc.options[:polymorphic]
          end

        else
          fields[key] = value
        end
      end
      fields
    end

    def order_clause(order)
      order.map {|pair| "#{pair[0]} #{pair[1]}"}.join(",")
    end
  end
end

ActiveSupport.on_load(:active_record) do
  extend ::OrmAdapter::ToAdapter
  self::OrmAdapter = ::OrmAdapter::ActiveRecord
end