Module: Cudify
- Included in:
- ActiveRecord::Base
- Defined in:
- lib/cudify.rb,
lib/cudify/version.rb
Overview
User: TsaiKoga Params: rec For example: { :id => 1, …, :_destroy => 1 } this record will be delete { :id => 2, …, :_destroy => 0 } || { :id => 2, … } these records will be updated { :id => nil, … } || { name: “TsaiKoga”, sex: “man”, … } these records will be created
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
Instance Method Details
#cudify(rec) ⇒ Object
15 16 17 |
# File 'lib/cudify.rb', line 15 def cudify rec define_cudify :cudify, rec end |
#cudify!(rec) ⇒ Object
11 12 13 |
# File 'lib/cudify.rb', line 11 def cudify! rec define_cudify :cudify!, rec end |
#define_cudify(name, rec) ⇒ Object
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 |
# File 'lib/cudify.rb', line 19 def define_cudify(name, rec) # At first, parameters hash become array array = (rec.is_a?(Array) ? rec : [rec]) records = [] # Secondly, records are created, updated or deleted transaction do array.each do |hash| if hash[:id] == nil then make = ( name == :cudify ? :create : :create! ) hash[:_destroy].to_i == 1 ? next : (record = send(make,hash)) records.push(record) else if hash[:_destroy].to_i == 1 then del = ( name == :cudify ? :delete : :destroy ) find(hash[:id]).nil? ? raise : find(hash[:id]).send(:destroy) else hash = hash.slice!(:_destroy) find(hash[:id]).nil? ? raise : find(hash[:id]).update_attributes!(hash) records.push(find(hash[:id])) end end end end # At last, return the records what are created or updated return records.compact end |