- MODULE Object::ROUTING
- CLASS Object::HashWithIndifferentAccess
- A
- B
- C
- D
- E
- H
- I
- L
- O
- P
- R
- S
- T
-
- test_a_bad_type_column,
- test_alt_becomes_works_with_sti,
- test_alt_complex_inheritance,
- test_alt_destroy_all_within_inheritance,
- test_alt_eager_loading,
- test_alt_find_first_within_inheritance,
- test_alt_finding_incorrect_type_data,
- test_alt_inheritance_condition,
- test_alt_inheritance_find,
- test_alt_inheritance_find_all,
- test_alt_inheritance_save,
- test_alt_update_all_within_inheritance,
- test_class_without_store_full_sti_class_returns_demodulized_name,
- test_company_descends_from_active_record,
- test_complex_inheritance,
- test_destroy_all_within_inheritance,
- test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option,
- test_eager_load_belongs_to_primary_key_quoting,
- test_eager_load_belongs_to_something_inherited,
- test_find_first_within_inheritance,
- test_finding_incorrect_type_data,
- test_homepage,
- test_inheritance_condition,
- test_inheritance_find,
- test_inheritance_find_all,
- test_inheritance_save,
- test_inheritance_without_mapping,
- test_inherits_custom_primary_key,
- test_scope_inherited_properly,
- test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled,
- test_should_store_full_class_name_with_store_full_sti_class_option_enabled,
- test_update_all_within_inheritance,
- to_json,
- to_param,
- to_query,
- try
- U
- W
- ActiveModel::Conversion
- Java
- FileUtils
- W3CValidators
| FIXTURE_LOAD_PATH | = | File.expand_path('fixtures', File.dirname(__FILE__)) |
| AppRoutes | = | ActionDispatch::Routing::RouteSet.new |
| MyHash | = | Class.new(Hash) |
| N | = | (ENV['N'] || 1000).to_i |
| ORIGINAL_LOCALES | = | I18n.available_locales.map {|locale| locale.to_s }.sort |
| FIXTURES | = | Pathname.new(FIXTURE_LOAD_PATH) |
| SharedTestRoutes | = | ActionDispatch::Routing::RouteSet.new |
| PATH_TO_AR | = | "#{File.dirname(__FILE__)}/../../activerecord/lib" |
| CACHE_DIR | = | 'test_cache' |
| FILE_STORE_PATH | = | File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) |
Don't change '/../temp/' cavalierly or you might hose something you don't want hosed |
||
| ROUTING | = | ActionDispatch::Routing |
#<RDoc::Comment:0x007fde7c9072b8> #<RDoc::Comment:0x007fde7ceacf38> #<RDoc::Comment:0x007fde7f2ed258> #<RDoc::Comment:0x007fde7f5e38b8> #<RDoc::Comment:0x007fde7f817710> #<RDoc::Comment:0x007fde7eeba320> #<RDoc::Comment:0x007fde7edfd900> |
||
| TEST_ROOT | = | File.expand_path(File.dirname(__FILE__)) |
| FIXTURES_ROOT | = | TEST_ROOT + "/fixtures" |
| SCHEMA_FILE | = | TEST_ROOT + "/schema.rb" |
| TIME | = | (ENV['BENCHMARK_TIME'] || 20).to_i |
| RECORDS | = | (ENV['BENCHMARK_RECORDS'] || TIME*1000).to_i |
| QUOTED_TYPE | = | ActiveRecord::Base.connection.quote_column_name('type') |
Quote “type” if it's a reserved word for the current connection. |
||
| ASSETS_ROOT | = | TEST_ROOT + "/assets" |
| MIGRATIONS_ROOT | = | TEST_ROOT + "/migrations" |
| SCHEMA_ROOT | = | TEST_ROOT + "/schema" |
| DeveloperSalary | = | Struct.new(:amount) |
| TIMES | = | (ENV['N'] || 10_000).to_i |
| MissingSourceFile | = | LoadError |
| HashWithIndifferentAccess | = | ActiveSupport::HashWithIndifferentAccess |
| RUBY_ENGINE | = | 'ruby' unless defined?(RUBY_ENGINE) |
| ORIG_ARGV | = | ARGV.dup |
| ApplicationController | = | 10 |
| Conflict | = | 2 |
| MultipleConstantFile | = | 10 |
| SiblingConstant | = | MultipleConstantFile * 2 |
| Payment | = | Struct.new(:price) |
| Somewhere | = | Struct.new(:street, :city) do attr_accessor :name end |
| Invoice | = | Struct.new(:client) do delegate :street, :city, :name, :to => :client, :prefix => true delegate :street, :city, :name, :to => :client, :prefix => :customer end |
| Project | = | Struct.new(:description, :person) do delegate :name, :to => :person, :allow_nil => true delegate :to_f, :to => :description, :allow_nil => true end |
| Developer | = | Struct.new(:client) do delegate :name, :to => :client, :prefix => nil end |
| Tester | = | Struct.new(:client) do delegate :name, :to => :client, :prefix => false end |
| MTIME_FIXTURES_PATH | = | File.expand_path("../fixtures", __FILE__) |
| DEFAULT_APP_FILES | = | %w( .gitignore Gemfile Rakefile config.ru app/assets/javascripts app/assets/stylesheets app/assets/images app/controllers app/helpers app/mailers app/models app/views/layouts config/environments config/initializers config/locales db doc lib lib/tasks lib/assets log script/rails test/fixtures test/functional test/integration test/performance test/unit vendor vendor/assets vendor/plugins tmp/cache tmp/cache/assets ) |
| ObjectHelper | = | Class.new |
| AnotherObjectHelperTest | = | Class.new |
| DEFAULT_PLUGIN_FILES | = | %w( .gitignore Gemfile Rakefile README.rdoc bukkits.gemspec MIT-LICENSE lib lib/bukkits.rb lib/tasks/bukkits_tasks.rake lib/bukkits/version.rb test/bukkits_test.rb test/test_helper.rb test/dummy ) |
| RAILS_FRAMEWORK_ROOT | = | File.expand_path("#{File.dirname(__FILE__)}/../../..") |
TODO: Remove setting this magic constant |
||
| RAILS_ISOLATED_ENGINE | = | true |
| FRAMEWORKS | = | %w( activesupport activemodel activerecord activeresource actionpack actionmailer railties ) |
A duck-type assistant method. For example, Active Support extends Date to define an acts_like_date? method, and extends Time to define acts_like_time?. As a result, we can do “x.acts_like?(:time)” and “x.acts_like?(:date)” to do duck-type-safe comparisons, since classes that we want to act like Time simply need to define an acts_like_time? method.
An object is blank if it's false, empty, or a whitespace string. For
example, “”, “ ”, nil, [], and {} are all blank.
This simplifies:
if address.nil? || address.empty?
…to:
if address.blank?
This is a predicate useful for the doc:guides task of applications.
Can you safely dup this object?
False for nil, false, true, symbols,
numbers, class and module objects; true otherwise.
Returns true if this object is included in the argument(s). Argument must
be any object which responds to #include? or optionally,
multiple arguments can be passed in. Usage:
characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true
character = "Konata"
character.in?("Konata", "Kagami", "Tsukasa") # => true
This will throw an ArgumentError if a single argument is passed in and it
doesn't respond to #include?.
# File activesupport/lib/active_support/core_ext/object/inclusion.rb, line 13 def in?(*args) if args.length > 1 args.include? self else another_object = args.first if another_object.respond_to? :include? another_object.include? self else raise ArgumentError.new("The single parameter passed to #in? must respond to #include?") end end end
# File activerecord/test/cases/helper.rb, line 117 def load_schema # silence verbose schema loading original_stdout = $stdout $stdout = StringIO.new adapter_name = ActiveRecord::Base.connection.adapter_name.downcase adapter_specific_schema_file = SCHEMA_ROOT + "/#{adapter_name}_specific_schema.rb" load SCHEMA_ROOT + "/schema.rb" if File.exists?(adapter_specific_schema_file) load adapter_specific_schema_file end ensure $stdout = original_stdout end
# File railties/lib/rails/commands/benchmarker.rb, line 8 def options options = {} defaults = ActiveSupport::Testing::Performance::DEFAULTS OptionParser.new do |opt| opt.banner = "Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]" opt.on('-r', '--runs N', Numeric, 'Number of runs.', "Default: #{defaults[:runs]}") { |r| options[:runs] = r } opt.on('-o', '--output PATH', String, 'Directory to use when writing the results.', "Default: #{defaults[:output]}") { |o| options[:output] = o } opt.on('-m', '--metrics a,b,c', Array, 'Metrics to use.', "Default: #{defaults[:metrics].join(",")}") { |m| options[:metrics] = m.map(&:to_sym) } opt.parse!(ARGV) end options end
# File activerecord/test/cases/ar_schema_test.rb, line 52 def perform_schema_define! ActiveRecord::Schema.define(:version => 7) do create_table :fruits do |t| t.column :color, :string t.column :fruit_size, :string # NOTE: "size" is reserved in Oracle t.column :texture, :string t.column :flavor, :string end end end
Returns object if it's present? otherwise returns
nil. object.presence is equivalent to
object.present? ? object : nil.
This is handy for any representation of objects where blank is the same as not present at all. For example, this simplifies a common check for HTTP POST/query parameters:
state = params[:state] if params[:state].present?
country = params[:country] if params[:country].present?
region = state || country || 'US'
…becomes:
region = params[:state].presence || params[:country].presence || 'US'
An object is present if it's not blank?.
ActionController::Base.use_accept_header = false
# File actionpack/examples/performance.rb, line 156 def run_all!(times, verbose) Runner.run(:overhead, times, verbose) Runner.run(:index, times, verbose) Runner.run(:template_1, times, verbose) Runner.run(:partial, times, verbose) Runner.run(:partial_10, times, verbose) Runner.run(:coll_10, times, verbose) Runner.run(:partial_100, times, verbose) Runner.run(:coll_100, times, verbose) Runner.run(:uniq_100, times, verbose) Runner.run(:diff_100, times, verbose) end
# File activeresource/test/abstract_unit.rb, line 16 def setup_response matz_hash = { 'person' => { :id => 1, :name => 'Matz' } } @default_request_headers = { 'Content-Type' => 'application/json' } @matz = matz_hash.to_json @matz_xml = matz_hash.to_xml @david = { :person => { :id => 2, :name => 'David' } }.to_json @greg = { :person => { :id => 3, :name => 'Greg' } }.to_json @addy = { :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }.to_json @rick = { :person => { :name => "Rick", :age => 25 } }.to_json @joe = { :person => { :id => 6, :name => 'Joe', :likes_hats => true }}.to_json @people = { :people => [ { :person => { :id => 1, :name => 'Matz' } }, { :person => { :id => 2, :name => 'David' } }] }.to_json @people_david = { :people => [ { :person => { :id => 2, :name => 'David' } }] }.to_json @addresses = { :addresses => [{ :address => { :id => 1, :street => '12345 Street', :country => 'Australia' } }] }.to_json # - deep nested resource - # - Luis (Customer) # - JK (Customer::Friend) # - Mateo (Customer::Friend::Brother) # - Edith (Customer::Friend::Brother::Child) # - Martha (Customer::Friend::Brother::Child) # - Felipe (Customer::Friend::Brother) # - Bryan (Customer::Friend::Brother::Child) # - Luke (Customer::Friend::Brother::Child) # - Eduardo (Customer::Friend) # - Sebas (Customer::Friend::Brother) # - Andres (Customer::Friend::Brother::Child) # - Jorge (Customer::Friend::Brother::Child) # - Elsa (Customer::Friend::Brother) # - Natacha (Customer::Friend::Brother::Child) # - Milena (Customer::Friend::Brother) # @luis = { :customer => { :id => 1, :name => 'Luis', :friends => [{ :name => 'JK', :brothers => [ { :name => 'Mateo', :children => [{ :name => 'Edith' },{ :name => 'Martha' }] }, { :name => 'Felipe', :children => [{ :name => 'Bryan' },{ :name => 'Luke' }] } ] }, { :name => 'Eduardo', :brothers => [ { :name => 'Sebas', :children => [{ :name => 'Andres' },{ :name => 'Jorge' }] }, { :name => 'Elsa', :children => [{ :name => 'Natacha' }] }, { :name => 'Milena', :children => [] } ] }] } }.to_json @startup_sound = { :sound => { :name => "Mac Startup Sound", :author => { :name => "Jim Reekes" } } }.to_json ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.json", {}, @matz mock.get "/people/1.xml", {}, @matz_xml mock.get "/people/2.xml", {}, @david mock.get "/people/Greg.json", {}, @greg mock.get "/people/6.json", {}, @joe mock.get "/people/4.json", { 'key' => 'value' }, nil, 404 mock.put "/people/1.json", {}, nil, 204 mock.delete "/people/1.json", {}, nil, 200 mock.delete "/people/2.xml", {}, nil, 400 mock.get "/people/99.json", {}, nil, 404 mock.post "/people.json", {}, @rick, 201, 'Location' => '/people/5.xml' mock.get "/people.json", {}, @people mock.get "/people/1/addresses.json", {}, @addresses mock.get "/people/1/addresses/1.json", {}, @addy mock.get "/people/1/addresses/2.xml", {}, nil, 404 mock.get "/people/2/addresses.json", {}, nil, 404 mock.get "/people/2/addresses/1.xml", {}, nil, 404 mock.get "/people/Greg/addresses/1.json", {}, @addy mock.put "/people/1/addresses/1.json", {}, nil, 204 mock.delete "/people/1/addresses/1.json", {}, nil, 200 mock.post "/people/1/addresses.json", {}, nil, 201, 'Location' => '/people/1/addresses/5' mock.get "/people/1/addresses/99.json", {}, nil, 404 mock.get "/people//addresses.xml", {}, nil, 404 mock.get "/people//addresses/1.xml", {}, nil, 404 mock.put "/people//addresses/1.xml", {}, nil, 404 mock.delete "/people//addresses/1.xml", {}, nil, 404 mock.post "/people//addresses.xml", {}, nil, 404 mock.head "/people/1.json", {}, nil, 200 mock.head "/people/Greg.json", {}, nil, 200 mock.head "/people/99.json", {}, nil, 404 mock.head "/people/1/addresses/1.json", {}, nil, 200 mock.head "/people/1/addresses/2.json", {}, nil, 404 mock.head "/people/2/addresses/1.json", {}, nil, 404 mock.head "/people/Greg/addresses/1.json", {}, nil, 200 # customer mock.get "/customers/1.json", {}, @luis # sound mock.get "/sounds/1.json", {}, @startup_sound end Person.user = nil Person.password = nil end
# File activerecord/test/cases/inheritance_test.rb, line 245 def switch_to_alt_inheritance_column # we don't want misleading test results, so get rid of the values in the type column Company.find(:all, :order => 'id').each do |c| c['type'] = nil c.save end [ Company, Firm, Client].each { |klass| klass.reset_column_information } Company.inheritance_column = 'ruby_type' end
# File activerecord/test/cases/inheritance_test.rb, line 75 def test_a_bad_type_column #SQLServer need to turn Identity Insert On before manually inserting into the Identity column if current_adapter?(:SybaseAdapter) Company.connection.execute "SET IDENTITY_INSERT companies ON" end Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')" #We then need to turn it back Off before continuing. if current_adapter?(:SybaseAdapter) Company.connection.execute "SET IDENTITY_INSERT companies OFF" end assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) } end
# File activerecord/test/cases/inheritance_test.rb, line 32 def test_class_without_store_full_sti_class_returns_demodulized_name old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = false assert_equal 'Company', Namespaced::Company.sti_name ensure ActiveRecord::Base.store_full_sti_class = old end
# File activerecord/test/cases/inheritance_test.rb, line 68 def test_company_descends_from_active_record assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? } assert AbstractCompany.descends_from_active_record?, 'AbstractCompany should descend from ActiveRecord::Base' assert Company.descends_from_active_record?, 'Company should descend from ActiveRecord::Base' assert !Class.new(Company).descends_from_active_record?, 'Company subclass should not descend from ActiveRecord::Base' end
# File activerecord/test/cases/inheritance_test.rb, line 196 def test_complex_inheritance very_special_client = VerySpecialClient.create("name" => "veryspecial") assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first assert_equal very_special_client, SpecialClient.find(:first, :conditions => "name = 'veryspecial'") assert_equal very_special_client, Company.find(:first, :conditions => "name = 'veryspecial'") assert_equal very_special_client, Client.find(:first, :conditions => "name = 'veryspecial'") assert_equal 1, Client.find(:all, :conditions => "name = 'Summit'").size assert_equal very_special_client, Client.find(very_special_client.id) end
# File activerecord/test/cases/inheritance_test.rb, line 58 def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = true item = Namespaced::Company.create :name => "Wolverine 2" assert_not_nil Company.find(item.id) assert_not_nil Namespaced::Company.find(item.id) ensure ActiveRecord::Base.store_full_sti_class = old end
# File activerecord/test/cases/inheritance_test.rb, line 184 def test_find_first_within_inheritance assert_kind_of Firm, Company.find(:first, :conditions => "name = '37signals'") assert_kind_of Firm, Firm.find(:first, :conditions => "name = '37signals'") assert_nil Client.find(:first, :conditions => "name = '37signals'") end
:output => 'tmp/performance', :formats => [:flat] }
# File activerecord/test/cases/inheritance_test.rb, line 89 def test_inheritance_find assert_kind_of Firm, Company.find(1), "37signals should be a firm" assert_kind_of Firm, Firm.find(1), "37signals should be a firm" assert_kind_of Client, Company.find(2), "Summit should be a client" assert_kind_of Client, Client.find(2), "Summit should be a client" end
# File activerecord/test/cases/inheritance_test.rb, line 234 def test_inheritance_without_mapping assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132") assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = 'roger'; s.save } end
# File activerecord/test/cases/inheritance_test.rb, line 40 def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = false item = Namespaced::Company.new assert_equal 'Company', item[:type] ensure ActiveRecord::Base.store_full_sti_class = old end
# File activerecord/test/cases/inheritance_test.rb, line 49 def test_should_store_full_class_name_with_store_full_sti_class_option_enabled old = ActiveRecord::Base.store_full_sti_class ActiveRecord::Base.store_full_sti_class = true item = Namespaced::Company.new assert_equal 'Namespaced::Company', item[:type] ensure ActiveRecord::Base.store_full_sti_class = old end
# File activerecord/test/cases/inheritance_test.rb, line 159 def test_update_all_within_inheritance Client.update_all "name = 'I am a client'" assert_equal "I am a client", Client.find(:all).first.name # Order by added as otherwise Oracle tests were failing because of different order of results assert_equal "37signals", Firm.find(:all, :order => "id").first.name end
Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
Alias of to_s.
Converts an object into a string suitable for use as a URL query string,
using the given key as the param name.
Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.
Invokes the method identified by the symbol method, passing it
any arguments and/or the block specified, just like the regular Ruby
Object#send does.
Unlike that method however, a NoMethodError
exception will not be raised and nil will be
returned instead, if the receiving object is a nil object or
NilClass.
If try is called without a method to call, it will yield any given block with the object.
Please also note that try is defined on Object,
therefore it won't work with subclasses of BasicObject.
For example, using try with SimpleDelegator will delegate
try to target instead of calling it on delegator itself.
Examples
Without try
@person && @person.name
or
@person ? @person.name : nil
With try
@person.try(:name)
try also accepts arguments and/or a block, for the method it
is trying
Person.try(:find, 1)
@people.try(:collect) {|p| p.name}
Without a method argument try will yield to the block unless the receiver is nil.
@person.try { |p| "#{p.first_name} #{p.last_name}" }
# File activesupport/lib/active_support/core_ext/uri.rb, line 12 def unescape(str, escaped = /%[a-fA-F\d]{2}/) # TODO: Are we actually sure that ASCII == UTF-8? # YK: My initial experiments say yes, but let's be sure please enc = str.encoding enc = Encoding::UTF_8 if enc == Encoding::US_ASCII str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(enc) end
An elegant way to factor duplication out of options passed to a series of
method calls. Each method called in the block, with the block variable as
the receiver, will have its options merged with the default
options hash provided. Each method called on the block
variable must take an options hash as its final argument.
Without with_options>, this code contains duplication:
class Account < ActiveRecord::Base
has_many :customers, :dependent => :destroy
has_many :products, :dependent => :destroy
has_many :invoices, :dependent => :destroy
has_many :expenses, :dependent => :destroy
end
Using with_options, we can remove the duplication:
class Account < ActiveRecord::Base
with_options :dependent => :destroy do |assoc|
assoc.has_many :customers
assoc.has_many :products
assoc.has_many :invoices
assoc.has_many :expenses
end
end
It can also be used with an explicit receiver:
I18n.with_options :locale => user.locale, :scope => "newsletter" do |i18n|
subject i18n.t :subject
body i18n.t :body, :user_name => user.name
end
with_options can also be nested since the call is forwarded to
its receiver. Each nesting level will merge inherited defaults in addition
to their own.