Sha256: a44a4632fb01e78f6057eac89e345518e3090fa6e06e49bcff5148a752fd0807

Contents?: true

Size: 1.53 KB

Versions: 108

Compression:

Stored size: 1.53 KB

Contents

module ActiveRecord

  # = Active Record Dynamic Finder Match
  #
  # Refer to ActiveRecord::Base documentation for Dynamic attribute-based finders for detailed info
  #
  class DynamicFinderMatch
    def self.match(method)
      finder       = :first
      bang         = false
      instantiator = nil

      case method.to_s
      when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
        finder = :last if $1 == 'last_'
        finder = :all if $1 == 'all_'
        names = $2
      when /^find_by_([_a-zA-Z]\w*)\!$/
        bang = true
        names = $1
      when /^find_or_create_by_([_a-zA-Z]\w*)\!$/
        bang = true
        instantiator = :create
        names = $1
      when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
        instantiator = $1 == 'initialize' ? :new : :create
        names = $2
      else
        return nil
      end

      new(finder, instantiator, bang, names.split('_and_'))
    end

    def initialize(finder, instantiator, bang, attribute_names)
      @finder          = finder
      @instantiator    = instantiator
      @bang            = bang
      @attribute_names = attribute_names
    end

    attr_reader :finder, :attribute_names, :instantiator

    def finder?
      @finder && !@instantiator
    end

    def instantiator?
      @finder == :first && @instantiator
    end

    def creator?
      @finder == :first && @instantiator == :create
    end

    def bang?
      @bang
    end

    def save_record?
      @instantiator == :create
    end

    def save_method
      bang? ? :save! : :save
    end
  end
end

Version data entries

108 entries across 85 versions & 13 rubygems

Version Path
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/dynamic_finder_match.rb
activerecord-3.2.13 lib/active_record/dynamic_finder_match.rb
activerecord-3.2.13.rc2 lib/active_record/dynamic_finder_match.rb
activerecord-3.2.13.rc1 lib/active_record/dynamic_finder_match.rb
active_mailer-0.0.5 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/dynamic_finder_match.rb
active_mailer-0.0.4 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/dynamic_finder_match.rb
active_mailer-0.0.3 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/dynamic_finder_match.rb
activerecord-3.2.12 lib/active_record/dynamic_finder_match.rb
challah-0.9.1.beta.3 vendor/bundle/gems/activerecord-3.2.11/lib/active_record/dynamic_finder_match.rb
devise_sociable-0.1.0 vendor/bundle/gems/activerecord-3.2.11/lib/active_record/dynamic_finder_match.rb
challah-0.9.1.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/dynamic_finder_match.rb
activerecord-3.2.11 lib/active_record/dynamic_finder_match.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.10/lib/active_record/dynamic_finder_match.rb
challah-0.9.0 vendor/bundle/gems/activerecord-3.2.9/lib/active_record/dynamic_finder_match.rb
activerecord-3.2.10 lib/active_record/dynamic_finder_match.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.7/lib/active_record/dynamic_finder_match.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/activerecord-3.2.7/lib/active_record/dynamic_finder_match.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/activerecord-3.2.9/lib/active_record/dynamic_finder_match.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.9/lib/active_record/dynamic_finder_match.rb
challah-rolls-0.2.0 vendor/bundle/gems/activerecord-3.2.8/lib/active_record/dynamic_finder_match.rb