Sha256: 28cb2f4bb35206e7b580ad1d8f192e6607caaa19a7eba3809e87ed81787b157a

Contents?: true

Size: 1.46 KB

Versions: 19

Compression:

Stored size: 1.46 KB

Contents

module Hobo
  
  IncludeInSave = classy_module do
    
    attr_accessor :included_in_save
    
    validate         :validate_included_in_save
    before_save      :save_included
    after_save       :clear_included_in_save    
    
    def include_in_save(association, *records)
      self.included_in_save ||= Hash.new {|h, k| h[k] = []}
      included_in_save[association.to_sym].concat records
    end
    
    def validate_included_in_save
      if included_in_save
        included_in_save._?.each_pair do |association, records|
          next if self.class.reflections[association].options[:validate]==false
          added = false
          records.each do |record|
            # we want to call valid? on each one, but only add the error to self once
            
            record.with_acting_user(acting_user) do
              unless record.valid?
                unless added
                  errors.add association, "..."
                  added = true
                end
              end
            end
          end
        end
      end
    end


    def save_included
      if included_in_save
        included_in_save.each_pair do |association, records|
          records.each do |record|
            # save_without_validation means without transactions too
            record.with_acting_user(acting_user) { record.save_without_validation }
          end
        end
      end
    end

    def clear_included_in_save
      included_in_save._?.clear
    end

  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
hobo-1.1.0 lib/hobo/include_in_save.rb
hobo-1.0.3 lib/hobo/include_in_save.rb
hobo-1.1.0.pre4 lib/hobo/include_in_save.rb
hobo-1.1.0.pre3 lib/hobo/include_in_save.rb
hobo-1.1.0.pre2 lib/hobo/include_in_save.rb
hobo-1.1.0.pre0 lib/hobo/include_in_save.rb
hobo-1.0.2 lib/hobo/include_in_save.rb
hobo-1.0.1 lib/hobo/include_in_save.rb
hobo-1.0.0 lib/hobo/include_in_save.rb
hobo-0.9.106 lib/hobo/include_in_save.rb
hobo-0.9.105 lib/hobo/include_in_save.rb
hobo-0.9.104 lib/hobo/include_in_save.rb
hobo-0.9.103 lib/hobo/include_in_save.rb
hobo-0.9.102 lib/hobo/include_in_save.rb
hobo-0.9.101 lib/hobo/include_in_save.rb
hobo-0.9.100 lib/hobo/include_in_save.rb
hobo-0.9.0 lib/hobo/include_in_save.rb
hobo-0.8.10 lib/hobo/include_in_save.rb
hobo-0.8.9 lib/hobo/include_in_save.rb