Module: Deferring

Defined in:
lib/deferring.rb,
lib/deferring/version.rb,
lib/deferring/deferred_association.rb,
lib/deferring/deferred_callback_listener.rb

Defined Under Namespace

Classes: DeferredAssociation, DeferredCallbackListener

Constant Summary collapse

VERSION =
'0.8.2'

Instance Method Summary collapse

Instance Method Details

#deferred_accepts_nested_attributes_for(*args) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/deferring.rb', line 48

def deferred_accepts_nested_attributes_for(*args)
  options = args.extract_options!
  reject_if = options.delete(:reject_if)
  accepts_nested_attributes_for(*args, options)

  association_name = args.first.to_s

  # teams_attributes=
  define_method :"#{association_name}_attributes=" do |attributes_collection|
    unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
      raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
    end

    # TODO: Implement :limit checks. Check rails source.

    if attributes_collection.is_a? Hash
      keys = attributes_collection.keys
      attributes_collection = if keys.include?('id') || keys.include?(:id)
        [attributes_collection]
      else
        attributes_collection.values
      end
    end

    attributes_collection.each do |attributes|
      attributes = attributes.with_indifferent_access

      if attributes['id'].blank?
        if !send(:"deferred_#{association_name}_reject_new_record?", attributes)
          send(:"#{association_name}").build(attributes.except(*deferred_unassignable_keys))
        end

      elsif existing_record = send(:"#{association_name}").detect { |record| record.id.to_s == attributes['id'].to_s }
        if !send(:"deferred_#{association_name}_call_reject_if", attributes)

          existing_record.attributes = attributes.except(*deferred_unassignable_keys)

          # TODO: Implement value_to_boolean code from rails for checking _destroy field.
          if attributes['_destroy'] == '1' && options[:allow_destroy]
            # remove from existing records and mark for destruction upon
            # saving
            send(:"#{association_name}").delete(existing_record)
            existing_record.mark_for_destruction
          end
        end

      else # new record referenced by id
        if !send(:"deferred_#{association_name}_call_reject_if", attributes)
          klass = self.class.reflect_on_association(:"#{association_name}").klass

          attribute_ids = attributes_collection.map { |a| a['id'] || a[:id] }.compact
          # TODO: Find out how to get send(:"#{association_name}").scoped.where working
          new_records = attribute_ids.empty? ? [] : klass.where(klass.primary_key => attribute_ids)
          new_record = new_records.detect { |record| record.id.to_s == attributes['id'].to_s }

          send(:"#{association_name}").push(new_record)
        end
      end
    end
  end

  # Determines if a new record should be build by checking for
  # has_destroy_flag? or if a <tt>:reject_if</tt> proc exists for this
  # association and evaluates to +true+.
  define_method :"deferred_#{association_name}_reject_new_record?" do |attributes|
    # TODO: Implement value_to_boolean code from rails for checking _destroy field.
    attributes['_destroy'] == '1' || send(:"deferred_#{association_name}_call_reject_if", attributes)
  end

  define_method :"deferred_#{association_name}_call_reject_if" do |attributes|
    return false if attributes['_destroy'] == '1'
    case callback = reject_if
    when Symbol
      method(callback).arity == 0 ? send(callback) : send(callback, attributes)
    when Proc
      callback.call(attributes)
    end
  end

  define_method :deferred_unassignable_keys do
    %w(_destroy id)
  end
end

#deferred_has_and_belongs_to_many(*args) ⇒ Object

Creates a wrapper around ‘has_and_belongs_to_many`. A normal habtm association is created, but this association is wrapped in a DeferredAssociation. The accessor methods of the original association are replaced with ones that will defer saving the association until the parent object has been saved.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/deferring.rb', line 11

def deferred_has_and_belongs_to_many(*args)
  options = args.extract_options!
  listeners = create_callback_listeners!(options)
  autosave = options.fetch(:autosave, true)
  validate = options.fetch(:validate, true)

  has_and_belongs_to_many(*args, **options)
  generate_deferred_association_methods(
    args.first.to_s,
    listeners,
    autosave: autosave,
    type: :habtm,
    validate: validate)
end

#deferred_has_many(*args) ⇒ Object

Creates a wrapper around ‘has_many`. A normal has many association is created, but this association is wrapped in a DeferredAssociation. The accessor methods of the original association are replaced with ones that will defer saving the association until the parent object has been saved.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deferring.rb', line 30

def deferred_has_many(*args)
  options = args.extract_options!
  listeners = create_callback_listeners!(options)
  inverse_association_name = options[:as] || options[:inverse_of] || self.name.underscore.to_sym
  autosave = options.fetch(:autosave, true)
  validate = options.fetch(:validate, true)

  has_many(*args, **options)
  generate_deferred_association_methods(
    args.first.to_s,
    listeners,
    inverse_association_name: inverse_association_name,
    autosave: autosave,
    type: :has_many,
    dependent: options[:dependent],
    validate: validate)
end