Module: ActiveRecord::FindByExtension
- Included in:
- Base
- Defined in:
- lib/vex/active_record/find_by_extension.rb
Instance Method Summary collapse
- #create_by!(args, opts, &block) ⇒ Object
- #find_all_by(args, opts = nil) ⇒ Object
- #find_by(args, opts = nil) ⇒ Object
- #find_by!(args, opts = nil) ⇒ Object
- #find_or_create_all_by(args, opts = nil, &block) ⇒ Object
- #find_or_create_by(args, opts = nil, &block) ⇒ Object
Instance Method Details
#create_by!(args, opts, &block) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/vex/active_record/find_by_extension.rb', line 20 def create_by!(args, opts, &block) args = opts.update(args) if opts obj = new args if block_given? yield(obj) end obj.save! obj end |
#find_all_by(args, opts = nil) ⇒ Object
2 3 4 5 6 |
# File 'lib/vex/active_record/find_by_extension.rb', line 2 def find_all_by(args, opts = nil) return find(:all, :conditions => args) if opts.nil? with_scope(:find => opts) do find_all_by(args) end end |
#find_by(args, opts = nil) ⇒ Object
8 9 10 11 12 |
# File 'lib/vex/active_record/find_by_extension.rb', line 8 def find_by(args, opts = nil) return find(:first, :conditions => args) if opts.nil? with_scope(:find => opts) do find_by(args) end end |
#find_by!(args, opts = nil) ⇒ Object
14 15 16 17 |
# File 'lib/vex/active_record/find_by_extension.rb', line 14 def find_by!(args, opts = nil) find_by(args, opts) || raise(ActiveRecord::RecordNotFound, "Couldn't find #{self} with #{args.inspect}") end |
#find_or_create_all_by(args, opts = nil, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vex/active_record/find_by_extension.rb', line 34 def find_or_create_all_by(args, opts = nil, &block) requested = args.cross models = find_all_by(args) return models if requested.length == models.length # TODO: Check locking connection.locked("#{self.name}#create") do models = find_all_by(args) return models if requested.length == models.length keys = args.keys missing = requested - models.map do |model| args.keys.inject({}) do |hash, key| hash.update key => model.send(key) end end # TODO: Potential mass insert, when no block given missing.each do |data| models << create_by!(data, opts, &block) end end models end |
#find_or_create_by(args, opts = nil, &block) ⇒ Object
30 31 32 |
# File 'lib/vex/active_record/find_by_extension.rb', line 30 def find_or_create_by(args, opts = nil, &block) find(:first, :conditions => args) || create_by!(args, opts, &block) end |