Class: ActiveNull::NullModelBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_null/null_model_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, overrides) ⇒ NullModelBuilder

Returns a new instance of NullModelBuilder.



7
8
9
10
# File 'lib/active_null/null_model_builder.rb', line 7

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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/active_null/null_model_builder.rb', line 5

def model
  @model
end

#overridesObject (readonly)

Returns the value of attribute overrides.



5
6
7
# File 'lib/active_null/null_model_builder.rb', line 5

def overrides
  @overrides
end

Instance Method Details

#buildObject



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
# File 'lib/active_null/null_model_builder.rb', line 12

def build
  model = self.model
  null = Naught.build do |config|
    config.impersonate model

    model.reflect_on_all_associations.each do |relation|
      if relation.collection?
        define_method(relation.name) { relation.klass.none }
      else
        define_method(relation.name) do
          return unless relation.klass.respond_to? :null
          relation.klass.null
        end
      end
    end

    model.column_defaults.each do |field, default|
      define_method(field.to_sym) { default }
    end

    def nil?
      true
    end

    def present?
      false
    end

    def blank?
      true
    end

    def to_json
      '{}'
    end

    if Object.const_defined? 'Draper'
      def decorate(options = {})
        decorator_class.decorate(self, options)
      end

      def decorator_class
        self.class.decorator_class
      end

      def decorator_class?
        self.class.decorator_class?
      end

      def applied_decorators
        []
      end

      def decorated_with?(decorator_class)
        false
      end

      def decorated?
        false
      end
    end
  end
  null.send(:include, Draper::Decoratable) if Object.const_defined? 'Draper'
  null.send(:include, overrides) if overrides
  set_null_model null
end

#full_nameObject



84
85
86
87
# File 'lib/active_null/null_model_builder.rb', line 84

def full_name
  return name if model.parent == Object
  "#{model.parent.name}::#{name}"
end

#nameObject



79
80
81
82
# File 'lib/active_null/null_model_builder.rb', line 79

def name
  base_name = model.name.split('::').last
  "Null#{base_name}"
end

#set_null_model(null) ⇒ Object



89
90
91
# File 'lib/active_null/null_model_builder.rb', line 89

def set_null_model(null)
  model.parent.const_set name, null
end