File: association.rb

package info (click to toggle)
ruby-rails-admin 0.8.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,492 kB
  • ctags: 1,292
  • sloc: ruby: 5,341; makefile: 3
file content (84 lines) | stat: -rw-r--r-- 1,956 bytes parent folder | download
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
83
84
module RailsAdmin
  module Adapters
    module ActiveRecord
      class Association
        attr_reader :association, :model

        def initialize(association, model)
          @association = association
          @model = model
        end

        def name
          association.name.to_sym
        end

        def pretty_name
          name.to_s.tr('_', ' ').capitalize
        end

        def type
          association.macro
        end

        def klass
          if options[:polymorphic]
            polymorphic_parents(:active_record, model.name.to_s, name) || []
          else
            association.klass
          end
        end

        def primary_key
          (options[:primary_key] || association.klass.primary_key).try(:to_sym) unless polymorphic?
        end

        def foreign_key
          association.foreign_key.to_sym
        end

        def foreign_key_nullable?
          return true if foreign_key.nil? || type != :has_many
          (column = klass.columns_hash[foreign_key.to_s]).nil? || column.null
        end

        def foreign_type
          options[:foreign_type].try(:to_sym) || :"#{name}_type" if options[:polymorphic]
        end

        def foreign_inverse_of
          nil
        end

        def as
          options[:as].try :to_sym
        end

        def polymorphic?
          options[:polymorphic] || false
        end

        def inverse_of
          options[:inverse_of].try :to_sym
        end

        def read_only?
          (klass.all.instance_eval(&scope).readonly_value if scope.is_a? Proc) ||
            association.nested? ||
            false
        end

        def nested_options
          model.nested_attributes_options.try { |o| o[name.to_sym] }
        end

        def association?
          true
        end

        delegate :options, :scope, to: :association, prefix: false
        delegate :polymorphic_parents, to: RailsAdmin::AbstractModel
      end
    end
  end
end