Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into jobs

* master:
  Less strict activerecord-deprecated_finders version
  Revert "Round usec when writing timestamp attribute."
  Add failing test case for #8460
  Lets kepp using Ruby 1.9 syntax
  Only check for unpermmited parameters if action_on_unpermitted_parameters is present
  Revert "Merge pull request #7873 from steveklabnik/update_initializer"
  Revert "Set the default timezone after the initialization since the configuration"
  Revert "updating i18n guide with locale config location."
  avoid creating an object in every call
  Remove unnecessary require
  Re-do Getting Started application with Rails 4.
  fixing link to validations guide in getting started guide
  Getting Started Guide: strong_parameters
  Fix images in getting started guide.
  Remove tabs, use spaces :scissors:
  Whitespaces :scissors:
  • Loading branch information...
commit f0f146d871b932b7fcbe548b2e5aa191cca3877a 2 parents 6802e37 + 5f41295
@tenderlove tenderlove authored
Showing with 527 additions and 423 deletions.
  1. +3 −2 actionpack/lib/action_controller/metal/strong_parameters.rb
  2. +20 −20 actionpack/test/controller/parameters/nested_parameters_test.rb
  3. +17 −17 actionpack/test/controller/parameters/parameters_permit_test.rb
  4. +2 −2 actionpack/test/dispatch/test_request_test.rb
  5. +1 −1  activerecord/activerecord.gemspec
  6. +5 −13 activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
  7. +12 −12 activerecord/lib/active_record/core.rb
  8. +4 −7 activerecord/test/cases/connection_pool_test.rb
  9. +6 −7 activerecord/test/cases/dirty_test.rb
  10. +14 −16 activesupport/lib/active_support/railtie.rb
  11. BIN  guides/assets/images/getting_started/forbidden_attributes_for_new_post.png
  12. BIN  guides/assets/images/getting_started/routing_error_no_controller.png
  13. BIN  guides/assets/images/getting_started/routing_error_no_route_matches.png
  14. BIN  guides/assets/images/getting_started/template_is_missing_posts_new.png
  15. BIN  guides/assets/images/getting_started/unknown_action_create_for_posts.png
  16. BIN  guides/assets/images/getting_started/unknown_action_new_for_posts.png
  17. +16 −0 guides/code/getting_started/.gitignore
  18. +12 −12 guides/code/getting_started/Gemfile
  19. +150 −0 guides/code/getting_started/Gemfile.lock
  20. +3 −3 guides/code/getting_started/README.rdoc
  21. +0 −1  guides/code/getting_started/Rakefile
  22. +1 −0  guides/code/getting_started/app/assets/javascripts/application.js
  23. +3 −0  guides/code/getting_started/app/assets/javascripts/comments.js.coffee
  24. +3 −0  guides/code/getting_started/app/assets/javascripts/posts.js.coffee
  25. +3 −0  guides/code/getting_started/app/assets/javascripts/welcome.js.coffee
  26. +1 −1  guides/code/getting_started/app/assets/stylesheets/application.css
  27. +3 −0  guides/code/getting_started/app/assets/stylesheets/comments.css.scss
  28. +3 −0  guides/code/getting_started/app/assets/stylesheets/posts.css.scss
  29. +3 −0  guides/code/getting_started/app/assets/stylesheets/welcome.css.scss
  30. +3 −1 guides/code/getting_started/app/controllers/application_controller.rb
  31. +3 −3 guides/code/getting_started/app/controllers/comments_controller.rb
  32. 0  guides/code/getting_started/{vendor/plugins/.gitkeep → app/controllers/concerns/.keep}
  33. +17 −17 guides/code/getting_started/app/controllers/posts_controller.rb
  34. 0  guides/code/getting_started/{test/unit/.gitkeep → app/mailers/.keep}
  35. 0  guides/code/getting_started/{test/integration/.gitkeep → app/models/.keep}
  36. 0  guides/code/getting_started/{test/functional/.gitkeep → app/models/concerns/.keep}
  37. +5 −4 guides/code/getting_started/app/models/post.rb
  38. +3 −3 guides/code/getting_started/app/views/comments/_comment.html.erb
  39. +2 −2 guides/code/getting_started/app/views/layouts/application.html.erb
  40. +14 −12 guides/code/getting_started/app/views/posts/_form.html.erb
  41. +4 −4 guides/code/getting_started/app/views/posts/edit.html.erb
  42. +6 −8 guides/code/getting_started/app/views/posts/index.html.erb
  43. +3 −3 guides/code/getting_started/app/views/posts/new.html.erb
  44. +3 −3 guides/code/getting_started/app/views/posts/show.html.erb
  45. +2 −1  guides/code/getting_started/app/views/welcome/index.html.erb
  46. +4 −0 guides/code/getting_started/bin/bundle
  47. +4 −0 guides/code/getting_started/bin/rails
  48. +4 −0 guides/code/getting_started/bin/rake
  49. +2 −37 guides/code/getting_started/config/application.rb
  50. +2 −2 guides/code/getting_started/config/environment.rb
  51. +7 −7 guides/code/getting_started/config/environments/development.rb
  52. +28 −11 guides/code/getting_started/config/environments/production.rb
  53. +7 −5 guides/code/getting_started/config/environments/test.rb
  54. +4 −0 guides/code/getting_started/config/initializers/filter_parameter_logging.rb
  55. +6 −5 guides/code/getting_started/config/initializers/inflections.rb
  56. 0  .../lib/rails/generators/rails/app/templates → guides/code/getting_started}/config/initializers/locale.rb
  57. +5 −2 guides/code/getting_started/config/initializers/secret_token.rb
  58. +1 −1  guides/code/getting_started/config/initializers/session_store.rb
  59. +6 −6 guides/code/getting_started/config/initializers/wrap_parameters.rb
  60. +20 −2 guides/code/getting_started/config/locales/en.yml
  61. +2 −58 guides/code/getting_started/config/routes.rb
  62. 0  guides/code/getting_started/db/migrate/{20120420083127_create_posts.rb → 20130122042648_create_posts.rb}
  63. +1 −2  ...ode/getting_started/db/migrate/{20110901012815_create_comments.rb → 20130122045842_create_comments.rb}
  64. +9 −18 guides/code/getting_started/db/schema.rb
  65. 0  guides/code/getting_started/{test/fixtures/.gitkeep → lib/assets/.keep}
  66. 0  guides/code/getting_started/lib/tasks/{.gitkeep → .keep}
  67. 0  guides/code/getting_started/{lib/assets/.gitkeep → log/.keep}
  68. +2 −1  guides/code/getting_started/public/404.html
  69. +1 −1  guides/code/getting_started/public/422.html
  70. +2 −1  guides/code/getting_started/public/500.html
  71. +0 −6 guides/code/getting_started/script/rails
  72. 0  guides/code/getting_started/{app/models/.gitkeep → test/controllers/.keep}
  73. 0  guides/code/getting_started/test/{functional → controllers}/comments_controller_test.rb
  74. +1 −1  guides/code/getting_started/test/{unit/tag_test.rb → controllers/posts_controller_test.rb}
  75. +1 −0  guides/code/getting_started/test/{functional → controllers}/welcome_controller_test.rb
  76. 0  guides/code/getting_started/{app/mailers/.gitkeep → test/fixtures/.keep}
  77. +3 −3 guides/code/getting_started/test/fixtures/comments.yml
  78. +1 −1  guides/code/getting_started/test/fixtures/posts.yml
  79. +0 −49 guides/code/getting_started/test/functional/posts_controller_test.rb
  80. 0  guides/code/getting_started/test/helpers/.keep
  81. 0  guides/code/getting_started/test/{unit → }/helpers/comments_helper_test.rb
  82. 0  guides/code/getting_started/test/{unit → }/helpers/posts_helper_test.rb
  83. +4 −0 guides/code/getting_started/test/helpers/welcome_helper_test.rb
  84. 0  guides/code/getting_started/test/integration/.keep
  85. 0  guides/code/getting_started/test/mailers/.keep
  86. 0  guides/code/getting_started/test/models/.keep
  87. 0  guides/code/getting_started/test/{unit → models}/comment_test.rb
  88. 0  guides/code/getting_started/test/{unit → models}/post_test.rb
  89. +2 −0  guides/code/getting_started/test/test_helper.rb
  90. +0 −4 guides/code/getting_started/test/unit/helpers/home_helper_test.rb
  91. 0  guides/code/getting_started/vendor/assets/javascripts/.keep
  92. 0  guides/code/getting_started/vendor/assets/stylesheets/.keep
  93. +38 −12 guides/source/getting_started.md
  94. +1 −1  guides/source/i18n.md
  95. +0 −1  railties/lib/rails/generators/rails/app/templates/bin/bundle
  96. +8 −0 railties/lib/rails/generators/rails/app/templates/config/application.rb
  97. +1 −11 railties/test/application/configuration_test.rb
View
5 actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -251,7 +251,7 @@ def permit(*filters)
end
end
- unpermitted_parameters!(params)
+ unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters
params.permit!
end
@@ -401,6 +401,7 @@ def array_of_permitted_scalars_filter(params, key)
end
end
+ EMPTY_ARRAY = []
def hash_filter(params, filter)
filter = filter.with_indifferent_access
@@ -408,7 +409,7 @@ def hash_filter(params, filter)
slice(*filter.keys).each do |key, value|
return unless value
- if filter[key] == []
+ if filter[key] == EMPTY_ARRAY
# Declaration { comment_ids: [] }.
array_of_permitted_scalars_filter(params, key)
else
View
40 actionpack/test/controller/parameters/nested_parameters_test.rb
@@ -16,7 +16,7 @@ def assert_filtered_out(params, key)
}, {
name: "Christopher Marlowe"
}, {
- :name => %w(malicious injected names)
+ name: %w(malicious injected names)
}],
details: {
pages: 200,
@@ -71,25 +71,25 @@ def assert_filtered_out(params, key)
test "nested arrays with strings" do
params = ActionController::Parameters.new({
- :book => {
- :genres => ["Tragedy"]
+ book: {
+ genres: ["Tragedy"]
}
})
- permitted = params.permit :book => {:genres => []}
+ permitted = params.permit book: {genres: []}
assert_equal ["Tragedy"], permitted[:book][:genres]
end
test "permit may specify symbols or strings" do
params = ActionController::Parameters.new({
- :book => {
- :title => "Romeo and Juliet",
- :author => "William Shakespeare"
+ book: {
+ title: "Romeo and Juliet",
+ author: "William Shakespeare"
},
- :magazine => "Shakespeare Today"
+ magazine: "Shakespeare Today"
})
- permitted = params.permit({:book => ["title", :author]}, "magazine")
+ permitted = params.permit({book: ["title", :author]}, "magazine")
assert_equal "Romeo and Juliet", permitted[:book][:title]
assert_equal "William Shakespeare", permitted[:book][:author]
assert_equal "Shakespeare Today", permitted[:magazine]
@@ -132,15 +132,15 @@ def assert_filtered_out(params, key)
test "fields_for-style nested params" do
params = ActionController::Parameters.new({
- :book => {
- :authors_attributes => {
- :'0' => { :name => 'William Shakespeare', :age_of_death => '52' },
- :'1' => { :name => 'Unattributed Assistant' },
- :'2' => { :name => %w(injected names)}
+ book: {
+ authors_attributes: {
+ :'0' => { name: 'William Shakespeare', age_of_death: '52' },
+ :'1' => { name: 'Unattributed Assistant' },
+ :'2' => { name: %w(injected names)}
}
}
})
- permitted = params.permit :book => { :authors_attributes => [ :name ] }
+ permitted = params.permit book: { authors_attributes: [ :name ] }
assert_not_nil permitted[:book][:authors_attributes]['0']
assert_not_nil permitted[:book][:authors_attributes]['1']
@@ -153,14 +153,14 @@ def assert_filtered_out(params, key)
test "fields_for-style nested params with negative numbers" do
params = ActionController::Parameters.new({
- :book => {
- :authors_attributes => {
- :'-1' => { :name => 'William Shakespeare', :age_of_death => '52' },
- :'-2' => { :name => 'Unattributed Assistant' }
+ book: {
+ authors_attributes: {
+ :'-1' => { name: 'William Shakespeare', age_of_death: '52' },
+ :'-2' => { name: 'Unattributed Assistant' }
}
}
})
- permitted = params.permit :book => { :authors_attributes => [:name] }
+ permitted = params.permit book: { authors_attributes: [:name] }
assert_not_nil permitted[:book][:authors_attributes]['-1']
assert_not_nil permitted[:book][:authors_attributes]['-2']
View
34 actionpack/test/controller/parameters/parameters_permit_test.rb
@@ -20,7 +20,7 @@ def assert_filtered_out(params, key)
end
test 'if nothing is permitted, the hash becomes empty' do
- params = ActionController::Parameters.new(:id => '1234')
+ params = ActionController::Parameters.new(id: '1234')
permitted = params.permit
assert permitted.permitted?
assert permitted.empty?
@@ -34,7 +34,7 @@ def assert_filtered_out(params, key)
values += [StringIO.new]
values.each do |value|
- params = ActionController::Parameters.new(:id => value)
+ params = ActionController::Parameters.new(id: value)
permitted = params.permit(:id)
assert_equal value, permitted[:id]
@@ -47,7 +47,7 @@ def assert_filtered_out(params, key)
end
test 'key: unknown keys are filtered out' do
- params = ActionController::Parameters.new(:id => '1234', :injected => 'injected')
+ params = ActionController::Parameters.new(id: '1234', injected: 'injected')
permitted = params.permit(:id)
assert_equal '1234', permitted[:id]
assert_filtered_out permitted, :injected
@@ -55,7 +55,7 @@ def assert_filtered_out(params, key)
test 'key: arrays are filtered out' do
[[], [1], ['1']].each do |array|
- params = ActionController::Parameters.new(:id => array)
+ params = ActionController::Parameters.new(id: array)
permitted = params.permit(:id)
assert_filtered_out permitted, :id
@@ -68,8 +68,8 @@ def assert_filtered_out(params, key)
end
test 'key: hashes are filtered out' do
- [{}, {:foo => 1}, {:foo => 'bar'}].each do |hash|
- params = ActionController::Parameters.new(:id => hash)
+ [{}, {foo: 1}, {foo: 'bar'}].each do |hash|
+ params = ActionController::Parameters.new(id: hash)
permitted = params.permit(:id)
assert_filtered_out permitted, :id
@@ -82,7 +82,7 @@ def assert_filtered_out(params, key)
end
test 'key: non-permitted scalar values are filtered out' do
- params = ActionController::Parameters.new(:id => Object.new)
+ params = ActionController::Parameters.new(id: Object.new)
permitted = params.permit(:id)
assert_filtered_out permitted, :id
@@ -94,37 +94,37 @@ def assert_filtered_out(params, key)
end
test 'key: it is not assigned if not present in params' do
- params = ActionController::Parameters.new(:name => 'Joe')
+ params = ActionController::Parameters.new(name: 'Joe')
permitted = params.permit(:id)
assert !permitted.has_key?(:id)
end
test 'key to empty array: empty arrays pass' do
- params = ActionController::Parameters.new(:id => [])
- permitted = params.permit(:id => [])
+ params = ActionController::Parameters.new(id: [])
+ permitted = params.permit(id: [])
assert_equal [], permitted[:id]
end
test 'key to empty array: arrays of permitted scalars pass' do
[['foo'], [1], ['foo', 'bar'], [1, 2, 3]].each do |array|
- params = ActionController::Parameters.new(:id => array)
- permitted = params.permit(:id => [])
+ params = ActionController::Parameters.new(id: array)
+ permitted = params.permit(id: [])
assert_equal array, permitted[:id]
end
end
test 'key to empty array: permitted scalar values do not pass' do
['foo', 1].each do |permitted_scalar|
- params = ActionController::Parameters.new(:id => permitted_scalar)
- permitted = params.permit(:id => [])
+ params = ActionController::Parameters.new(id: permitted_scalar)
+ permitted = params.permit(id: [])
assert_filtered_out permitted, :id
end
end
test 'key to empty array: arrays of non-permitted scalar do not pass' do
- [[Object.new], [[]], [[1]], [{}], [{:id => '1'}]].each do |non_permitted_scalar|
- params = ActionController::Parameters.new(:id => non_permitted_scalar)
- permitted = params.permit(:id => [])
+ [[Object.new], [[]], [[1]], [{}], [{id: '1'}]].each do |non_permitted_scalar|
+ params = ActionController::Parameters.new(id: non_permitted_scalar)
+ permitted = params.permit(id: [])
assert_filtered_out permitted, :id
end
end
View
4 actionpack/test/dispatch/test_request_test.rb
@@ -40,10 +40,10 @@ class TestRequestTest < ActiveSupport::TestCase
req.cookie_jar["login"] = "XJ-122"
assert_cookies({"user_name" => "david", "login" => "XJ-122"}, req.cookie_jar)
- assert_nothing_raised do
+ assert_nothing_raised do
req.cookie_jar["login"] = nil
assert_cookies({"user_name" => "david", "login" => nil}, req.cookie_jar)
- end
+ end
req.cookie_jar.delete(:login)
assert_cookies({"user_name" => "david"}, req.cookie_jar)
View
2  activerecord/activerecord.gemspec
@@ -25,5 +25,5 @@ Gem::Specification.new do |s|
s.add_dependency 'activemodel', version
s.add_dependency 'arel', '~> 3.0.2'
- s.add_dependency 'activerecord-deprecated_finders', '0.0.2'
+ s.add_dependency 'activerecord-deprecated_finders', '~> 0.0.3'
end
View
18 activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -39,14 +39,11 @@ def #{attr_name}=(original_time)
unless time.acts_like?(:time)
time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
end
- zoned_time = time && time.in_time_zone rescue nil
- rounded_time = round_usec(zoned_time)
- rounded_value = round_usec(read_attribute("#{attr_name}"))
- if (rounded_value != rounded_time) || (!rounded_value && original_time)
- write_attribute("#{attr_name}", original_time)
- #{attr_name}_will_change!
- @attributes_cache["#{attr_name}"] = zoned_time
- end
+ time = time.in_time_zone rescue nil if time
+ changed = read_attribute(:#{attr_name}) != time
+ write_attribute(:#{attr_name}, original_time)
+ #{attr_name}_will_change! if changed
+ @attributes_cache["#{attr_name}"] = time
end
EOV
generated_attribute_methods.module_eval(method_body, __FILE__, line)
@@ -62,11 +59,6 @@ def create_time_zone_conversion_attribute?(name, column)
[:datetime, :timestamp].include?(column.type)
end
end
-
- private
- def round_usec(value)
- value.change(usec: 0) if value
- end
end
end
end
View
24 activerecord/lib/active_record/core.rb
@@ -365,18 +365,18 @@ def init_internals
pk = self.class.primary_key
@attributes[pk] = nil unless @attributes.key?(pk)
- @aggregation_cache = {}
- @association_cache = {}
- @attributes_cache = {}
- @previously_changed = {}
- @changed_attributes = {}
- @readonly = false
- @destroyed = false
- @marked_for_destruction = false
- @new_record = true
- @txn = nil
- @_start_transaction_state = {}
- @transaction = nil
+ @aggregation_cache = {}
+ @association_cache = {}
+ @attributes_cache = {}
+ @previously_changed = {}
+ @changed_attributes = {}
+ @readonly = false
+ @destroyed = false
+ @marked_for_destruction = false
+ @new_record = true
+ @txn = nil
+ @_start_transaction_state = {}
+ @transaction = nil
end
end
end
View
11 activerecord/test/cases/connection_pool_test.rb
@@ -328,19 +328,16 @@ def test_pool_sets_connection_visitor
assert @pool.connection.visitor.is_a?(Arel::Visitors::ToSql)
end
-
- #make sure exceptions are thrown when establish_connection
- #is called with a anonymous class
+ # make sure exceptions are thrown when establish_connection
+ # is called with a anonymous class
def test_anonymous_class_exception
anonymous = Class.new(ActiveRecord::Base)
handler = ActiveRecord::Base.connection_handler
-
- assert_raises(RuntimeError){
+
+ assert_raises(RuntimeError) {
handler.establish_connection anonymous, nil
}
end
-
-
end
end
end
View
13 activerecord/test/cases/dirty_test.rb
@@ -551,18 +551,17 @@ def test_field_named_field
end
end
- def test_setting_time_attributes_with_time_zone_field_to_same_time_should_not_be_marked_as_a_change
+ def test_datetime_attribute_can_be_updated_with_fractional_seconds
in_time_zone 'Paris' do
target = Class.new(ActiveRecord::Base)
- target.table_name = 'pirates'
+ target.table_name = 'topics'
- created_on = Time.now
+ written_on = Time.utc(2012, 12, 1, 12, 0, 0).in_time_zone('Paris')
- pirate = target.create(:created_on => created_on)
- pirate.reload # Here mysql truncate the usec value to 0
+ topic = target.create(:written_on => written_on)
+ topic.written_on += 0.3
- pirate.created_on = created_on
- assert !pirate.created_on_changed?
+ assert topic.written_on_changed?, 'Fractional second update not detected'
end
end
View
30 activesupport/lib/active_support/railtie.rb
@@ -13,6 +13,20 @@ class Railtie < Rails::Railtie # :nodoc:
end
end
+ # Sets the default value for Time.zone
+ # If assigned value cannot be matched to a TimeZone, an exception will be raised.
+ initializer "active_support.initialize_time_zone" do |app|
+ require 'active_support/core_ext/time/zones'
+ zone_default = Time.find_zone!(app.config.time_zone)
+
+ unless zone_default
+ raise 'Value assigned to config.time_zone not recognized. ' \
+ 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
+ end
+
+ Time.zone_default = zone_default
+ end
+
# Sets the default week start
# If assigned value is not a valid day symbol (e.g. :sunday, :monday, ...), an exception will be raised.
initializer "active_support.initialize_beginning_of_week" do |app|
@@ -28,21 +42,5 @@ class Railtie < Rails::Railtie # :nodoc:
ActiveSupport.send(k, v) if ActiveSupport.respond_to? k
end
end
-
- # Sets the default value for Time.zone after initialization since the default configuration
- # lives in application initializers.
- # If assigned value cannot be matched to a TimeZone, an exception will be raised.
- config.after_initialize do |app|
- require 'active_support/core_ext/time/zones'
- zone_default = Time.find_zone!(app.config.time_zone)
-
- unless zone_default
- raise 'Value assigned to config.time_zone not recognized. ' \
- 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
- end
-
- Time.zone_default = zone_default
- end
-
end
end
View
BIN  guides/assets/images/getting_started/forbidden_attributes_for_new_post.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  guides/assets/images/getting_started/routing_error_no_controller.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  guides/assets/images/getting_started/routing_error_no_route_matches.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  guides/assets/images/getting_started/template_is_missing_posts_new.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  guides/assets/images/getting_started/unknown_action_create_for_posts.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  guides/assets/images/getting_started/unknown_action_new_for_posts.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
16 guides/code/getting_started/.gitignore
@@ -0,0 +1,16 @@
+# See http://help.github.com/ignore-files/ for more about ignoring files.
+#
+# If you find yourself ignoring temporary files generated by your text editor
+# or operating system, you probably want to add a global ignore instead:
+# git config --global core.excludesfile '~/.gitignore_global'
+
+# Ignore bundler config.
+/.bundle
+
+# Ignore the default SQLite database.
+/db/*.sqlite3
+/db/*.sqlite3-journal
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
View
24 guides/code/getting_started/Gemfile
@@ -1,38 +1,38 @@
source 'https://rubygems.org'
-gem 'rails', '3.2.3'
-
-# Bundle edge Rails instead:
-# gem 'rails', :git => 'git://github.com/rails/rails.git'
+gem 'rails', '4.0.0'
gem 'sqlite3'
-
# Gems used only for assets and not required
# in production environments by default.
group :assets do
- gem 'sass-rails', '~> 3.2.3'
- gem 'coffee-rails', '~> 3.2.1'
+ gem 'sprockets-rails'
+ gem 'sass-rails'
+ gem 'coffee-rails'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
- # gem 'therubyracer', :platform => :ruby
+ # gem 'therubyracer', platforms: :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
+# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
+gem 'turbolinks'
+
+# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
+gem 'jbuilder', '~> 1.0.1'
+
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
-# To use Jbuilder templates for JSON
-# gem 'jbuilder'
-
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
-# gem 'capistrano'
+# gem 'capistrano', group: :development
# To use debugger
# gem 'debugger'
View
150 guides/code/getting_started/Gemfile.lock
@@ -0,0 +1,150 @@
+GIT
+ remote: git://github.com/rails/activerecord-deprecated_finders.git
+ revision: 2e7b35d7948cefb2bba96438873d7f7bb1961a03
+ specs:
+ activerecord-deprecated_finders (0.0.2)
+
+GIT
+ remote: git://github.com/rails/arel.git
+ revision: 38d0a222e275d917a2c1d093b24457bafb600a00
+ specs:
+ arel (3.0.2.20120819075748)
+
+GIT
+ remote: git://github.com/rails/coffee-rails.git
+ revision: 052634e6d02d4800d7b021201cc8d5829775b3cd
+ specs:
+ coffee-rails (4.0.0.beta)
+ coffee-script (>= 2.2.0)
+ railties (>= 4.0.0.beta, < 5.0)
+
+GIT
+ remote: git://github.com/rails/sass-rails.git
+ revision: ae8138a89cac397c0df903dd533e2862902ce8f5
+ specs:
+ sass-rails (4.0.0.beta)
+ railties (>= 4.0.0.beta, < 5.0)
+ sass (>= 3.1.10)
+ sprockets-rails (~> 2.0.0.rc0)
+ tilt (~> 1.3)
+
+GIT
+ remote: git://github.com/rails/sprockets-rails.git
+ revision: 09917104fdb42245fe369612a7b0e3d77e1ba763
+ specs:
+ sprockets-rails (2.0.0.rc1)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ sprockets (~> 2.8)
+
+PATH
+ remote: /Users/steve/src/rails
+ specs:
+ actionmailer (4.0.0.beta)
+ actionpack (= 4.0.0.beta)
+ mail (~> 2.5.3)
+ actionpack (4.0.0.beta)
+ activesupport (= 4.0.0.beta)
+ builder (~> 3.1.0)
+ erubis (~> 2.7.0)
+ rack (~> 1.4.3)
+ rack-test (~> 0.6.1)
+ activemodel (4.0.0.beta)
+ activesupport (= 4.0.0.beta)
+ builder (~> 3.1.0)
+ activerecord (4.0.0.beta)
+ activemodel (= 4.0.0.beta)
+ activerecord-deprecated_finders (= 0.0.2)
+ activesupport (= 4.0.0.beta)
+ arel (~> 3.0.2)
+ activesupport (4.0.0.beta)
+ i18n (~> 0.6)
+ minitest (~> 4.1)
+ multi_json (~> 1.3)
+ thread_safe (~> 0.1)
+ tzinfo (~> 0.3.33)
+ rails (4.0.0.beta)
+ actionmailer (= 4.0.0.beta)
+ actionpack (= 4.0.0.beta)
+ activerecord (= 4.0.0.beta)
+ activesupport (= 4.0.0.beta)
+ bundler (>= 1.2.2, < 2.0)
+ railties (= 4.0.0.beta)
+ sprockets-rails (~> 2.0.0.rc1)
+ railties (4.0.0.beta)
+ actionpack (= 4.0.0.beta)
+ activesupport (= 4.0.0.beta)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (>= 0.15.4, < 2.0)
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ atomic (1.0.1)
+ builder (3.1.4)
+ coffee-script (2.2.0)
+ coffee-script-source
+ execjs
+ coffee-script-source (1.4.0)
+ erubis (2.7.0)
+ execjs (1.4.0)
+ multi_json (~> 1.0)
+ hike (1.2.1)
+ i18n (0.6.1)
+ jbuilder (1.0.2)
+ activesupport (>= 3.0.0)
+ jquery-rails (2.2.0)
+ railties (>= 3.0, < 5.0)
+ thor (>= 0.14, < 2.0)
+ json (1.7.6)
+ mail (2.5.3)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.19)
+ minitest (4.4.0)
+ multi_json (1.5.0)
+ polyglot (0.3.3)
+ rack (1.4.4)
+ rack-test (0.6.2)
+ rack (>= 1.0)
+ rake (10.0.3)
+ rdoc (3.12)
+ json (~> 1.4)
+ sass (3.2.5)
+ sprockets (2.8.2)
+ hike (~> 1.2)
+ multi_json (~> 1.0)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.7)
+ thor (0.16.0)
+ thread_safe (0.1.0)
+ atomic
+ tilt (1.3.3)
+ treetop (1.4.12)
+ polyglot
+ polyglot (>= 0.3.1)
+ turbolinks (1.0.0)
+ coffee-rails
+ tzinfo (0.3.35)
+ uglifier (1.3.0)
+ execjs (>= 0.3.0)
+ multi_json (~> 1.0, >= 1.0.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activerecord-deprecated_finders!
+ arel!
+ coffee-rails!
+ jbuilder (~> 1.0.1)
+ jquery-rails
+ rails!
+ sass-rails!
+ sprockets-rails!
+ sqlite3
+ turbolinks
+ uglifier (>= 1.0.3)
View
6 guides/code/getting_started/README.rdoc
@@ -23,6 +23,6 @@ Things you may want to cover:
* ...
-If you plan to generate application documentation with `rake doc:app` this file
-is expected to be `README.rdoc`, otherwise please feel free to rename it and use
-a different markup language.
+
+Please feel free to use a different markup language if you do not plan to run
+<tt>rake doc:app</tt>.
View
1  guides/code/getting_started/Rakefile
@@ -1,4 +1,3 @@
-#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
View
1  guides/code/getting_started/app/assets/javascripts/application.js
@@ -12,4 +12,5 @@
//
//= require jquery
//= require jquery_ujs
+//= require turbolinks
//= require_tree .
View
3  guides/code/getting_started/app/assets/javascripts/comments.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
View
3  guides/code/getting_started/app/assets/javascripts/posts.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
View
3  guides/code/getting_started/app/assets/javascripts/welcome.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
View
2  guides/code/getting_started/app/assets/stylesheets/application.css
@@ -10,4 +10,4 @@
*
*= require_self
*= require_tree .
-*/
+ */
View
3  guides/code/getting_started/app/assets/stylesheets/comments.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Comments controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3  guides/code/getting_started/app/assets/stylesheets/posts.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the posts controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
3  guides/code/getting_started/app/assets/stylesheets/welcome.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the welcome controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
4 guides/code/getting_started/app/controllers/application_controller.rb
@@ -1,3 +1,5 @@
class ApplicationController < ActionController::Base
- protect_from_forgery
+ # Prevent CSRF attacks by raising an exception.
+ # For APIs, you may want to use :null_session instead.
+ protect_from_forgery with: :exception
end
View
6 guides/code/getting_started/app/controllers/comments_controller.rb
@@ -1,9 +1,10 @@
class CommentsController < ApplicationController
- http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
+  http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
+
def create
@post = Post.find(params[:post_id])
- @comment = @post.comments.create(params[:comment])
+ @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end
@@ -13,5 +14,4 @@ def destroy
@comment.destroy
redirect_to post_path(@post)
end
-
end
View
0  ...e/getting_started/vendor/plugins/.gitkeep → ...ng_started/app/controllers/concerns/.keep
File renamed without changes
View
34 guides/code/getting_started/app/controllers/posts_controller.rb
@@ -1,7 +1,7 @@
class PostsController < ApplicationController
- http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
-
+  http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
+
def index
@posts = Post.all
end
@@ -10,31 +10,31 @@ def show
@post = Post.find(params[:id])
end
- def new
- @post = Post.new
+ def edit
+ @post = Post.find(params[:id])
end
- def create
- @post = Post.new(params[:post])
+ def update
+ @post = Post.find(params[:id])
- if @post.save
- redirect_to :action => :show, :id => @post.id
+ if @post.update(params[:post].permit(:title, :text))
+ redirect_to action: :show, id: @post.id
else
- render 'new'
+ render 'edit'
end
end
- def edit
- @post = Post.find(params[:id])
+ def new
+ @post = Post.new
end
- def update
- @post = Post.find(params[:id])
+ def create
+ @post = Post.new(params[:post].permit(:title, :text))
- if @post.update(params[:post])
- redirect_to :action => :show, :id => @post.id
+ if @post.save
+ redirect_to action: :show, id: @post.id
else
- render 'edit'
+ render 'new'
end
end
@@ -42,6 +42,6 @@ def destroy
@post = Post.find(params[:id])
@post.destroy
- redirect_to :action => :index
+ redirect_to action: :index
end
end
View
0  ...s/code/getting_started/test/unit/.gitkeep → ...es/code/getting_started/app/mailers/.keep
File renamed without changes
View
0  ...getting_started/test/integration/.gitkeep → guides/code/getting_started/app/models/.keep
File renamed without changes
View
0  .../getting_started/test/functional/.gitkeep → ...getting_started/app/models/concerns/.keep
File renamed without changes
View
9 guides/code/getting_started/app/models/post.rb
@@ -1,6 +1,7 @@
class Post < ActiveRecord::Base
- validates :title, :presence => true,
- :length => { :minimum => 5 }
-
- has_many :comments, :dependent => :destroy
+ has_many :comments, dependent: :destroy
+
+ validates :title,
+ presence: true,
+ length: { minimum: 5 }
end
View
6 guides/code/getting_started/app/views/comments/_comment.html.erb
@@ -2,7 +2,7 @@
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
-
+
<p>
<strong>Comment:</strong>
<%= comment.body %>
@@ -10,6 +10,6 @@
<p>
<%= link_to 'Destroy Comment', [comment.post, comment],
- :method => :delete,
- :data => { :confirm => 'Are you sure?' } %>
+ method: :delete,
+ data: { confirm: 'Are you sure?' } %>
</p>
View
4 guides/code/getting_started/app/views/layouts/application.html.erb
@@ -2,8 +2,8 @@
<html>
<head>
<title>Blog</title>
- <%= stylesheet_link_tag "application", :media => "all" %>
- <%= javascript_include_tag "application" %>
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
View
26 guides/code/getting_started/app/views/posts/_form.html.erb
@@ -1,25 +1,27 @@
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
- <div id="errorExplanation">
- <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
- <ul>
- <% @post.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
+ <div id="errorExplanation">
+ <h2><%= pluralize(@post.errors.count, "error") %> prohibited
+ this post from being saved:</h2>
+ <ul>
+ <% @post.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
<% end %>
<p>
- <%= f.label :title %><br />
+ <%= f.label :title %><br>
<%= f.text_field :title %>
</p>
-
+
<p>
- <%= f.label :text %><br />
+ <%= f.label :text %><br>
<%= f.text_area :text %>
</p>
-
+
<p>
<%= f.submit %>
</p>
<% end %>
+
View
8 guides/code/getting_started/app/views/posts/edit.html.erb
@@ -1,5 +1,5 @@
-<h1>Editing post</h1>
-
+<h1>Edit post</h1>
+
<%= render 'form' %>
-
-<%= link_to 'Back', :action => :index %>
+
+<%= link_to 'Back', action: :index %>
View
14 guides/code/getting_started/app/views/posts/index.html.erb
@@ -1,7 +1,4 @@
-<h1>Listing posts</h1>
-
-<%= link_to 'New post', :action => :new %>
-
+<h1>Listing Posts</h1>
<table>
<tr>
<th>Title</th>
@@ -10,14 +7,15 @@
<th></th>
<th></th>
</tr>
-
+
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
- <td><%= link_to 'Show', :action => :show, :id => post.id %>
- <td><%= link_to 'Edit', :action => :edit, :id => post.id %>
- <td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :data => { :confirm => 'Are you sure?' } %>
+ <td><%= link_to 'Show', action: :show, id: post.id %></td>
+ <td><%= link_to 'Edit', action: :edit, id: post.id %></td>
+ <td><%= link_to 'Destroy', { action: :destroy, id: post.id },
+ method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
View
6 guides/code/getting_started/app/views/posts/new.html.erb
@@ -1,5 +1,5 @@
<h1>New post</h1>
-
+
<%= render 'form' %>
-
-<%= link_to 'Back', :action => :index %>
+
+<%= link_to 'Back', action: :index %>
View
6 guides/code/getting_started/app/views/posts/show.html.erb
@@ -2,7 +2,7 @@
<strong>Title:</strong>
<%= @post.title %>
</p>
-
+
<p>
<strong>Text:</strong>
<%= @post.text %>
@@ -10,9 +10,9 @@
<h2>Comments</h2>
<%= render @post.comments %>
-
+
<h2>Add a comment:</h2>
<%= render "comments/form" %>
-
+
<%= link_to 'Edit Post', edit_post_path(@post) %> |
<%= link_to 'Back to Posts', posts_path %>
View
3  guides/code/getting_started/app/views/welcome/index.html.erb
@@ -1,2 +1,3 @@
<h1>Hello, Rails!</h1>
-<%= link_to "My Blog", :controller => "posts" %>
+
+<%= link_to "My Blog", controller: "posts" %>
View
4 guides/code/getting_started/bin/bundle
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
+require 'rubygems'
+load Gem.bin_path('bundler', 'bundle')
View
4 guides/code/getting_started/bin/rails
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+APP_PATH = File.expand_path('../../config/application', __FILE__)
+require_relative '../config/boot'
+require 'rails/commands'
View
4 guides/code/getting_started/bin/rake
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+require_relative '../config/boot'
+require 'rake'
+Rake.application.run
View
39 guides/code/getting_started/config/application.rb
@@ -2,12 +2,8 @@
require 'rails/all'
-if defined?(Bundler)
- # If you precompile assets before deploying to production, use this line
- Bundler.require(*Rails.groups(:assets => %w(development test)))
- # If you want your assets lazily compiled in production, use this line
- # Bundler.require(:default, :assets, Rails.env)
-end
+# Assets should be precompiled for production (so we don't need the gems loaded then)
+Bundler.require(*Rails.groups(assets: %w(development test)))
module Blog
class Application < Rails::Application
@@ -17,36 +13,5 @@ class Application < Rails::Application
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
-
- # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
- # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
- # config.time_zone = 'Central Time (US & Canada)'
-
- # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
- # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
- # config.i18n.default_locale = :de
-
- # Configure the default encoding used in templates for Ruby 1.9.
- config.encoding = "utf-8"
-
- # Configure sensitive parameters which will be filtered from the log file.
- config.filter_parameters += [:password]
-
- # Use SQL instead of Active Record's schema dumper when creating the database.
- # This is necessary if your schema can't be completely dumped by the schema dumper,
- # like if you have constraints or database-specific column types.
- # config.active_record.schema_format = :sql
-
- # Enforce whitelist mode for mass assignment.
- # This will create an empty whitelist of attributes available for mass-assignment for all models
- # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
- # parameters by using an attr_accessible or attr_protected declaration.
- # config.active_record.whitelist_attributes = true
-
- # Enable the asset pipeline.
- config.assets.enabled = true
-
- # Version of your assets, change this if you want to expire all your assets.
- config.assets.version = '1.0'
end
end
View
4 guides/code/getting_started/config/environment.rb
@@ -1,5 +1,5 @@
-# Load the rails application
+# Load the rails application.
require File.expand_path('../application', __FILE__)
-# Initialize the rails application
+# Initialize the rails application.
Blog::Application.initialize!
View
14 guides/code/getting_started/config/environments/development.rb
@@ -2,10 +2,13 @@
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
- # every request. This slows down response time but is perfect for development
+ # every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
+ # Do not eager load code on boot.
+ config.eager_load = false
+
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
@@ -19,16 +22,13 @@
# Only use best-standards-support built into browsers.
config.action_dispatch.best_standards_support = :builtin
- # Raise exception on mass assignment protection for ActiveRecord models.
- config.active_record.mass_assignment_sanitizer = :strict
-
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
config.active_record.auto_explain_threshold_in_seconds = 0.5
- # Do not compress assets.
- config.assets.compress = false
+ # Raise an error on page load if there are pending migrations
+ config.active_record.migration_error = :page_load
- # Expands the lines which load the assets.
+ # Debug mode disables concatenation and preprocessing of assets.
config.assets.debug = true
end
View
39 guides/code/getting_started/config/environments/production.rb
@@ -4,24 +4,36 @@
# Code is not reloaded between requests.
config.cache_classes = true
+ # Eager load code on boot. This eager loads most of Rails and
+ # your application in memory, allowing both thread web servers
+ # and those relying on copy on write to perform better.
+ # Rake tasks automatically ignore this option for performance.
+ config.eager_load = true
+
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
+ # Enable Rack::Cache to put a simple HTTP cache in front of your application
+ # Add `rack-cache` to your Gemfile before enabling this.
+ # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
+ # config.action_dispatch.rack_cache = true
+
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
# Compress JavaScripts and CSS.
- config.assets.compress = true
+ config.assets.js_compressor = :uglifier
+ # config.assets.css_compressor = :sass
- # Don't fallback to assets pipeline if a precompiled asset is missed.
+ # Whether to fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
- # Defaults to nil
- # config.assets.manifest = YOUR_PATH
+ # Version of your assets, change this if you want to expire all your assets.
+ config.assets.version = '1.0'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
@@ -30,8 +42,8 @@
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
- # See everything in the log (default is :info).
- # config.log_level = :debug
+ # Set to :debug to see everything in the log.
+ config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
@@ -45,15 +57,14 @@
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.action_controller.asset_host = "http://assets.example.com"
- # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added).
+ # Precompile additional assets.
+ # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# config.assets.precompile += %w( search.js )
- # Disable delivery errors, bad email addresses will be ignored.
+ # Ignore bad email addresses and do not raise email delivery errors.
+ # Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
- # Enable threaded mode.
- # config.threadsafe!
-
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found).
config.i18n.fallbacks = true
@@ -64,4 +75,10 @@
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL).
# config.active_record.auto_explain_threshold_in_seconds = 0.5
+
+ # Disable automatic flushing of the log to improve performance.
+ # config.autoflush_log = false
+
+ # Use default logging formatter so that PID and timestamp are not suppressed.
+ config.log_formatter = ::Logger::Formatter.new
end
View
12 guides/code/getting_started/config/environments/test.rb
@@ -2,11 +2,16 @@
# Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's
- # test suite. You never need to work with it otherwise. Remember that
+ # test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
- # and recreated between test runs. Don't rely on the data there!
+ # and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
+ # Do not eager load code on boot. This avoids loading your whole application
+ # just for the purpose of running a single test. If you are using a tool that
+ # preloads Rails for running tests, you may have to set it to true.
+ config.eager_load = false
+
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
@@ -26,9 +31,6 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
- # Raise exception on mass assignment protection for Active Record models.
- config.active_record.mass_assignment_sanitizer = :strict
-
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
end
View
4 guides/code/getting_started/config/initializers/filter_parameter_logging.rb
@@ -0,0 +1,4 @@
+# Be sure to restart your server when you modify this file.
+
+# Configure sensitive parameters which will be filtered from the log file.
+Rails.application.config.filter_parameters += [:password]
View
11 guides/code/getting_started/config/initializers/inflections.rb
@@ -1,15 +1,16 @@
# Be sure to restart your server when you modify this file.
-# Add new inflection rules using the following format
-# (all these examples are active by default):
-# ActiveSupport::Inflector.inflections do |inflect|
+# Add new inflection rules using the following format. Inflections
+# are locale specific, and you may define rules for as many different
+# locales as you wish. All of these examples are active by default:
+# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
-#
+
# These inflection rules are supported but not enabled by default:
-# ActiveSupport::Inflector.inflections do |inflect|
+# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end
View
0  ...p/templates/config/initializers/locale.rb → ...ing_started/config/initializers/locale.rb
File renamed without changes
View
7 guides/code/getting_started/config/initializers/secret_token.rb
@@ -2,8 +2,11 @@
# Your secret key for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
+
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
-# Make sure your secret key is kept private
+# You can use `rake secret` to generate a secure secret key.
+
+# Make sure your secret_key_base is kept private
# if you're sharing your code publicly.
-Blog::Application.config.secret_key_base = '685a9bf865b728c6549a191c90851c1b5ec41ecb60b9e94ad79dd3f824749798aa7b5e94431901960bee57809db0947b481570f7f13376b7ca190fa28099c459'
+Blog::Application.config.secret_key_base = 'e8aab50cec8a06a75694111a4cbaf6e22fc288ccbc6b268683aae7273043c69b15ca07d10c92a788dd6077a54762cbfcc55f19c3459f7531221b3169f8171a53'
View
2  guides/code/getting_started/config/initializers/session_store.rb
@@ -1,3 +1,3 @@
# Be sure to restart your server when you modify this file.
-Blog::Application.config.session_store :cookie_store, key: '_blog_session'
+Blog::Application.config.session_store :encrypted_cookie_store, key: '_blog_session'
View
12 guides/code/getting_started/config/initializers/wrap_parameters.rb
@@ -1,14 +1,14 @@
# Be sure to restart your server when you modify this file.
-#
+
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
- wrap_parameters format: [:json]
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
end
-# Disable root element in JSON by default.
-ActiveSupport.on_load(:active_record) do
- self.include_root_in_json = false
-end
+# To enable root element in JSON for ActiveRecord objects.
+# ActiveSupport.on_load(:active_record) do
+# self.include_root_in_json = true
+# end
View
22 guides/code/getting_started/config/locales/en.yml
@@ -1,5 +1,23 @@
-# Sample localization file for English. Add more files in this directory for other locales.
-# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
+# Files in the config/locales directory are used for internationalization
+# and are automatically loaded by Rails. If you want to use locales other
+# than English, add the necessary files in this directory.
+#
+# To use the locales, use `I18n.t`:
+#
+# I18n.t 'hello'
+#
+# In views, this is aliased to just `t`:
+#
+# <%= t('hello') %>
+#
+# To use a different locale, set it with `I18n.locale`:
+#
+# I18n.locale = :es
+#
+# This would use the information in config/locales/es.yml.
+#
+# To learn more, please read the Rails Internationalization guide
+# available at http://guides.rubyonrails.org/i18n.html.
en:
hello: "Hello world"
View
60 guides/code/getting_started/config/routes.rb
@@ -1,63 +1,7 @@
Blog::Application.routes.draw do
-
resources :posts do
resources :comments
end
-
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', as: :purchase
- # This route can be invoked with purchase_url(id: product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', on: :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- root :to => "welcome#index"
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id))(.:format)'
+
+ root to: "welcome#index"
end
View
0  ...db/migrate/20120420083127_create_posts.rb → ...db/migrate/20130122042648_create_posts.rb
File renamed without changes
View
3  ...migrate/20110901012815_create_comments.rb → ...migrate/20130122045842_create_comments.rb
@@ -3,10 +3,9 @@ def change
create_table :comments do |t|
t.string :commenter
t.text :body
- t.references :post
+ t.references :post, index: true
t.timestamps
end
- add_index :comments, :post_id
end
end
View
27 guides/code/getting_started/db/schema.rb
@@ -9,34 +9,25 @@
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
-# It's strongly recommended to check this file into your version control system.
+# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120420083127) do
+ActiveRecord::Schema.define(version: 20130122045842) do
- create_table "comments", :force => true do |t|
+ create_table "comments", force: true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
end
- add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
+ add_index "comments", ["post_id"], name: "index_comments_on_post_id"
- create_table "posts", :force => true do |t|
+ create_table "posts", force: true do |t|
t.string "title"
t.text "text"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
end
- create_table "tags", :force => true do |t|
- t.string "name"
- t.integer "post_id"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
- end
-
- add_index "tags", ["post_id"], :name => "index_tags_on_post_id"
-
end
View
0  ...de/getting_started/test/fixtures/.gitkeep → guides/code/getting_started/lib/assets/.keep
File renamed without changes
View
0  ...s/code/getting_started/lib/tasks/.gitkeep → guides/code/getting_started/lib/tasks/.keep
File renamed without changes
View
0  .../code/getting_started/lib/assets/.gitkeep → guides/code/getting_started/log/.keep
File renamed without changes
View
3  guides/code/getting_started/public/404.html
@@ -2,7 +2,7 @@
<html>
<head>
<title>The page you were looking for doesn't exist (404)</title>
- <style type="text/css">
+ <style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
@@ -22,5 +22,6 @@
<h1>The page you were looking for doesn't exist.</h1>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
+ <p>If you are the application owner check the logs for more information.</p>
</body>
</html>
View
2  guides/code/getting_started/public/422.html
@@ -2,7 +2,7 @@
<html>
<head>
<title>The change you wanted was rejected (422)</title>
- <style type="text/css">
+ <style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
View
3  guides/code/getting_started/public/500.html
@@ -2,7 +2,7 @@
<html>
<head>
<title>We're sorry, but something went wrong (500)</title>
- <style type="text/css">
+ <style>
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
@@ -21,5 +21,6 @@
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
</div>
+ <p>If you are the application owner check the logs for more information.</p>
</body>
</html>
View
6 guides/code/getting_started/script/rails
@@ -1,6 +0,0 @@
-#!/usr/bin/env ruby
-# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
-
-APP_PATH = File.expand_path('../../config/application', __FILE__)
-require File.expand_path('../../config/boot', __FILE__)
-require 'rails/commands'
View
0  .../code/getting_started/app/models/.gitkeep → ...de/getting_started/test/controllers/.keep
File renamed without changes
View
0  ...st/functional/comments_controller_test.rb → ...t/controllers/comments_controller_test.rb
File renamed without changes
View
2  ...ode/getting_started/test/unit/tag_test.rb → ...test/controllers/posts_controller_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'
-class TagTest < ActiveSupport::TestCase
+class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
View
1  ...est/functional/welcome_controller_test.rb → ...st/controllers/welcome_controller_test.rb
@@ -5,4 +5,5 @@ class WelcomeControllerTest < ActionController::TestCase
get :index
assert_response :success
end
+
end
View
0  ...code/getting_started/app/mailers/.gitkeep → .../code/getting_started/test/fixtures/.keep
File renamed without changes
View
6 guides/code/getting_started/test/fixtures/comments.yml
@@ -1,11 +1,11 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
commenter: MyString
body: MyText
- post:
+ post_id:
two:
commenter: MyString
body: MyText
- post:
+ post_id:
View
2  guides/code/getting_started/test/fixtures/posts.yml
@@ -1,4 +1,4 @@
-# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
one:
title: MyString
View
49 guides/code/getting_started/test/functional/posts_controller_test.rb
@@ -1,49 +0,0 @@
-require 'test_helper'
-
-class PostsControllerTest < ActionController::TestCase
- setup do
- @post = posts(:one)
- end
-
- test "should get index" do
- get :index
- assert_response :success
- assert_not_nil assigns(:posts)
- end
-
- test "should get new" do
- get :new
- assert_response :success
- end
-
- test "should create post" do
- assert_difference('Post.count') do
- post :create, post: @post.attributes
- end
-
- assert_redirected_to post_path(assigns(:post))
- end
-
- test "should show post" do
- get :show, id: @post.to_param
- assert_response :success
- end
-
- test "should get edit" do
- get :edit, id: @post.to_param
- assert_response :success
- end
-
- test "should update post" do
- put :update, id: @post.to_param, post: @post.attributes
- assert_redirected_to post_path(assigns(:post))
- end
-
- test "should destroy post" do
- assert_difference('Post.count', -1) do
- delete :destroy, id: @post.to_param
- end
-
- assert_redirected_to posts_path
- end
-end
View
0  guides/code/getting_started/test/helpers/.keep
No changes.
View
0  ...test/unit/helpers/comments_helper_test.rb → ...rted/test/helpers/comments_helper_test.rb
File renamed without changes
View
0  ...ed/test/unit/helpers/posts_helper_test.rb → ...started/test/helpers/posts_helper_test.rb
File renamed without changes
View
4 guides/code/getting_started/test/helpers/welcome_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class WelcomeHelperTest < ActionView::TestCase
+end
View
0  guides/code/getting_started/test/integration/.keep
No changes.
View
0  guides/code/getting_started/test/mailers/.keep
No changes.
View
0  guides/code/getting_started/test/models/.keep
No changes.
View
0  ...getting_started/test/unit/comment_test.rb → ...tting_started/test/models/comment_test.rb
File renamed without changes
View
0  ...de/getting_started/test/unit/post_test.rb → .../getting_started/test/models/post_test.rb
File renamed without changes
View
2  guides/code/getting_started/test/test_helper.rb
@@ -3,6 +3,8 @@
require 'rails/test_help'
class ActiveSupport::TestCase
+ ActiveRecord::Migration.check_pending!
+
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
View
4 guides/code/getting_started/test/unit/helpers/home_helper_test.rb
@@ -1,4 +0,0 @@
-require 'test_helper'
-
-class HomeHelperTest < ActionView::TestCase
-end
View
0  guides/code/getting_started/vendor/assets/javascripts/.keep
No changes.
View
0  guides/code/getting_started/vendor/assets/stylesheets/.keep
No changes.
View
50 guides/source/getting_started.md
@@ -583,9 +583,31 @@ content:
</p>
```
-Finally, if you now go to
-<http://localhost:3000/posts/new> you'll
-be able to create a post. Try it!
+If you now go to
+<http://localhost:3000/posts/new> you'll *almost* be able to create a post. Try
+it! You should get an error that looks like this:
+
+![Forbidden attributes for new post](images/getting_started/forbidden_attributes_for_new_post.png)
+
+Rails has several security features that help you write secure applications,
+and you're running into one of them now. This one is called
+'strong_parameters,' which requires us to tell Rails exactly which parameters
+we want to accept in our controllers. In this case, we want to allow the
+'title' and 'text' parameters, so change your `create` controller action to
+look like this:
+
+```
+ def create
+ @post = Post.new(params[:post].permit(:title, :text))
+
+ @post.save
+ redirect_to action: :show, id: @post.id
+ end
+```
+
+See the `permit`? It allows us to accept both `title` and `text` in this
+action. With this change, you should finally be able to create new `Post`s.
+Visit <http://localhost:3000/posts/new> and give it a try!
![Show action for posts](images/getting_started/show_action_for_posts.png)
@@ -711,10 +733,11 @@ class Post < ActiveRecord::Base
end
```
-These changes will ensure that all posts have a title that is at least five characters long.
-Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their
-format, and the existence of associated objects. Validations are covered in detail
-in [Active Record Validations and Callbacks](active_record_validations_callbacks.html#validations-overview)
+These changes will ensure that all posts have a title that is at least five
+characters long. Rails can validate a variety of conditions in a model,
+including the presence or uniqueness of columns, their format, and the
+existence of associated objects. Validations are covered in detail in [Active
+Record Validations](active_record_validations.html)
With the validation now in place, when you call `@post.save` on an invalid
post, it will return `false`. If you open `app/controllers/posts_controller.rb`
@@ -729,7 +752,7 @@ def new
end
def create
- @post = Post.new(params[:post])
+ @post = Post.new(params[:post].permit(:title, :text))
if @post.save
redirect_to action: :show, id: @post.id
@@ -864,8 +887,8 @@ method: :patch do |f| %>
This time we point the form to the `update` action, which is not defined yet
but will be very soon.
-The `method: :patch` option tells Rails that we want this form to be
-submitted via the `PUT` HTTP method which is the HTTP method you're expected to use to
+The `method: :patch` option tells Rails that we want this form to be submitted
+via the `PATCH` HTTP method which is the HTTP method you're expected to use to
**update** resources according to the REST protocol.
TIP: By default forms built with the _form_for_ helper are sent via `POST`.
@@ -883,7 +906,7 @@ And then create the `update` action in `app/controllers/posts_controller.rb`:
def update
@post = Post.find(params[:id])
- if @post.update(params[:post])
+ if @post.update(params[:post].permit(:title, :text))
redirect_to action: :show, id: @post.id
else
render 'edit'
@@ -1388,7 +1411,7 @@ Let's wire up the `create` in `app/controllers/comments_controller.rb`:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
- @comment = @post.comments.create(params[:comment])
+ @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end
end
@@ -1559,6 +1582,9 @@ Then you make the `app/views/posts/show.html.erb` look like the following:
<%= @post.text %>
</p>
+<h2>Comments</h2>
+<%= render @post.comments %>
+
<h2>Add a comment:</h2>
<%= render "comments/form" %>
View
2  guides/source/i18n.md
@@ -102,7 +102,7 @@ The **translations load path** (`I18n.load_path`) is just a Ruby Array of paths
NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.
-The default initializer `locale.rb` file has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines.
+The default `application.rb` files has instructions on how to add locales from another directory and how to set a different default locale. Just uncomment and edit the specific lines.
```ruby
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
View
1  railties/lib/rails/generators/rails/app/templates/bin/bundle
@@ -1,3 +1,2 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
-require 'rubygems'
load Gem.bin_path('bundler', 'bundle')
View
8 railties/lib/rails/generators/rails/app/templates/config/application.rb
@@ -22,6 +22,14 @@ class Application < Rails::Application
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
<% if options.skip_sprockets? -%>
# Disable the asset pipeline.
View
12 railties/test/application/configuration_test.rb
@@ -417,17 +417,7 @@ def index
require "#{app_path}/config/environment"
- assert_equal Time.find_zone!("Wellington"), Time.zone_default
- end
-
- test "timezone can be set on initializers" do
- app_file "config/initializers/locale.rb", <<-RUBY
- Rails.application.config.time_zone = "Central Time (US & Canada)"
- RUBY
-
- require "#{app_path}/config/environment"
-
- assert_equal Time.find_zone!("Central Time (US & Canada)"), Time.zone_default
+ assert_equal "Wellington", Rails.application.config.time_zone