diff options
-rw-r--r-- | actioncable/README.md | 51 | ||||
-rw-r--r-- | actionpack/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionview/lib/action_view/helpers/asset_tag_helper.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/lookup_context.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/rendering.rb | 2 | ||||
-rw-r--r-- | actionview/lib/action_view/template/types.rb | 29 | ||||
-rw-r--r-- | guides/source/active_record_postgresql.md | 4 | ||||
-rw-r--r-- | guides/source/association_basics.md | 16 |
8 files changed, 67 insertions, 43 deletions
diff --git a/actioncable/README.md b/actioncable/README.md index f58c8fdb16..636f9935cf 100644 --- a/actioncable/README.md +++ b/actioncable/README.md @@ -298,11 +298,12 @@ See the [rails/actioncable-examples](http://github.com/rails/actioncable-example ## Configuration -Action Cable has two required configurations: the Redis connection and specifying allowed request origins. +Action Cable has three required configurations: the Redis connection, allowed request origins, and the cable server url (which can optionally be set on the client side). ### Redis -By default, `ActionCable::Server::Base` will look for a configuration file in `Rails.root.join('config/redis/cable.yml')`. The file must follow the following format: +By default, `ActionCable::Server::Base` will look for a configuration file in `Rails.root.join('config/redis/cable.yml')`. +This file must specify a Redis url for each Rails environment. It may use the following format: ```yaml production: &production @@ -312,8 +313,7 @@ development: &development test: *development ``` -This format allows you to specify one configuration per Rails environment. You can also change the location of the Redis config file in -a Rails initializer with something like: +You can also change the location of the Redis config file in a Rails initializer with something like: ```ruby Rails.application.paths.add "config/redis/cable", with: "somewhere/else/cable.yml" @@ -327,29 +327,31 @@ Action Cable will only accept requests from specified origins, which are passed ActionCable.server.config.allowed_request_origins = ['http://rubyonrails.com', /http:\/\/ruby.*/] ``` +When running in the development environment, this defaults to "http://localhost:3000". + To disable and allow requests from any origin: ```ruby ActionCable.server.config.disable_request_forgery_protection = true ``` -By default, Action Cable allows all requests from localhost:3000 when running in the development environment. +### Consumer Configuration -### Other Configurations +Once you have decided how to run your cable server (see below), you must provide the server url (or path) to your client-side setup. +There are two ways you can do this. -The other common option to configure is the log tags applied to the per-connection logger. Here's close to what we're using in Basecamp: +The first is to simply pass it in when creating your consumer. For a standalone server, +this would be something like: `App.cable = ActionCable.createConsumer("ws://example.com:28080")`, and for an in-app server, +something like: `App.cable = ActionCable.createConsumer("/cable")`. -```ruby -ActionCable.server.config.log_tags = [ - -> request { request.env['bc.account_id'] || "no-account" }, - :action_cable, - -> request { request.uuid } -] -``` +The second option is to pass the server url through the `action_cable_meta_tag` in your layout. +This uses a url or path typically set via `config.action_cable.url` in the environment configuration files, or defaults to "/cable". -Your websocket url might change between environments. If you host your production server via https, you will need to use the wss scheme +This method is especially useful if your websocket url might change between environments. If you host your production server via https, you will need to use the wss scheme for your ActionCable server, but development might remain http and use the ws scheme. You might use localhost in development and your -domain in production. In any case, to vary the websocket url between environments, add the following configuration to each environment: +domain in production. + +In any case, to vary the websocket url between environments, add the following configuration to each environment: ```ruby config.action_cable.url = "ws://example.com:28080" @@ -367,6 +369,18 @@ And finally, create your consumer like so: App.cable = ActionCable.createConsumer() ``` +### Other Configurations + +The other common option to configure is the log tags applied to the per-connection logger. Here's close to what we're using in Basecamp: + +```ruby +ActionCable.server.config.log_tags = [ + -> request { request.env['bc.account_id'] || "no-account" }, + :action_cable, + -> request { request.uuid } +] +``` + For a full list of all configuration options, see the `ActionCable::Server::Configuration` class. Also note that your server must provide at least the same number of database connections as you have workers. The default worker pool is set to 100, so that means you have to make at least that available. You can change that in `config/database.yml` through the `pool` attribute. @@ -394,8 +408,7 @@ Then you start the server using a binstub in bin/cable ala: bundle exec puma -p 28080 cable/config.ru ``` -The above will start a cable server on port 28080. Remember to point your client-side setup against that using something like: -`App.cable = ActionCable.createConsumer("ws://basecamp.dev:28080")`. +The above will start a cable server on port 28080. ### In app @@ -408,8 +421,6 @@ Example::Application.routes.draw do end ``` -You can use `App.cable = ActionCable.createConsumer()` to connect to the cable server if `action_cable_meta_tag` is included in the layout. A custom path is specified as first argument to `createConsumer` (e.g. `App.cable = ActionCable.createConsumer("/websocket")`). - For every instance of your server you create and for every worker your server spawns, you will also have a new instance of ActionCable, but the use of Redis keeps messages synced across connections. ### Notes diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index b47e73377c..e31838db2f 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -80,7 +80,7 @@ https://github.com/rails/rails/pull/18334#issuecomment-69234050 we want `protect_from_forgery` to default to `prepend: false`. - `protect_from_forgery` will now be insterted into the callback chain at the + `protect_from_forgery` will now be inserted into the callback chain at the point it is called in your application. This is useful for cases where you want to `protect_from_forgery` after you perform required authentication callbacks or other callbacks that are required to run after forgery protection. @@ -354,7 +354,7 @@ * Allow `Bearer` as token-keyword in `Authorization-Header`. - Aditionally to `Token`, the keyword `Bearer` is acceptable as a keyword + Additionally to `Token`, the keyword `Bearer` is acceptable as a keyword for the auth-token. The `Bearer` keyword is described in the original OAuth RFC and used in libraries like Angular-JWT. diff --git a/actionview/lib/action_view/helpers/asset_tag_helper.rb b/actionview/lib/action_view/helpers/asset_tag_helper.rb index 91e934cd64..cc54faa778 100644 --- a/actionview/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionview/lib/action_view/helpers/asset_tag_helper.rb @@ -136,7 +136,7 @@ module ActionView tag( "link", "rel" => tag_options[:rel] || "alternate", - "type" => tag_options[:type] || Mime[type].to_s, + "type" => tag_options[:type] || Template::Types[type].to_s, "title" => tag_options[:title] || type.to_s.upcase, "href" => url_options.is_a?(Hash) ? url_for(url_options.merge(:only_path => false)) : url_options ) diff --git a/actionview/lib/action_view/lookup_context.rb b/actionview/lib/action_view/lookup_context.rb index d3935788ef..63a3c4ea5e 100644 --- a/actionview/lib/action_view/lookup_context.rb +++ b/actionview/lib/action_view/lookup_context.rb @@ -67,7 +67,7 @@ module ActionView def self.get(details) if details[:formats] details = details.dup - details[:formats] &= Mime::SET.symbols + details[:formats] &= Template::Types.symbols end @details_keys[details] ||= new end diff --git a/actionview/lib/action_view/rendering.rb b/actionview/lib/action_view/rendering.rb index 8604637da2..3ca7f9d220 100644 --- a/actionview/lib/action_view/rendering.rb +++ b/actionview/lib/action_view/rendering.rb @@ -84,7 +84,7 @@ module ActionView end def rendered_format - Mime[lookup_context.rendered_format] + Template::Types[lookup_context.rendered_format] end private diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index be45fcf742..c233d06ccb 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -5,19 +5,12 @@ module ActionView class Template class Types class Type - cattr_accessor :types - self.types = Set.new - - def self.register(*t) - types.merge(t.map(&:to_s)) - end - - register :html, :text, :js, :css, :xml, :json + SET = Struct.new(:symbols).new([ :html, :text, :js, :css, :xml, :json ]) def self.[](type) - return type if type.is_a?(self) - - if type.is_a?(Symbol) || types.member?(type.to_s) + if type.is_a?(self) + type + else new(type) end end @@ -28,16 +21,18 @@ module ActionView @symbol = symbol.to_sym end - delegate :to_s, :to_sym, :to => :symbol + def to_s + @symbol.to_s + end alias to_str to_s def ref - to_sym || to_s + @symbol end + alias to_sym ref def ==(type) - return false if type.blank? - symbol.to_sym == type.to_sym + @symbol == type.to_sym unless type.blank? end end @@ -52,6 +47,10 @@ module ActionView def self.[](type) type_klass[type] end + + def self.symbols + type_klass::SET.symbols + end end end end diff --git a/guides/source/active_record_postgresql.md b/guides/source/active_record_postgresql.md index b592209d4b..68c6a77882 100644 --- a/guides/source/active_record_postgresql.md +++ b/guides/source/active_record_postgresql.md @@ -84,6 +84,7 @@ Book.where("array_length(ratings, 1) >= 3") ### Hstore * [type definition](http://www.postgresql.org/docs/current/static/hstore.html) +* [functions and operators](http://www.postgresql.org/docs/current/static/hstore.html#AEN167712) NOTE: You need to enable the `hstore` extension to use hstore. @@ -108,6 +109,9 @@ profile.settings # => {"color"=>"blue", "resolution"=>"800x600"} profile.settings = {"color" => "yellow", "resolution" => "1280x1024"} profile.save! + +Profile.where("settings->'color' = ?", "yellow") +#=> #<ActiveRecord::Relation [#<Profile id: 1, settings: {"color"=>"yellow", "resolution"=>"1280x1024"}>]> ``` ### JSON diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index d83dda7228..520b324ea7 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -1872,11 +1872,21 @@ If you want to make sure that, upon insertion, all of the records in the persisted association are distinct (so that you can be sure that when you inspect the association that you will never find duplicate records), you should add a unique index on the table itself. For example, if you have a table named -`person_articles` and you want to make sure all the articles are unique, you could -add the following in a migration: +`readings` and you want to make sure the articles can only be added to a person once, +you could add the following in a migration: ```ruby -add_index :person_articles, :article, unique: true +add_index :readings, [:person_id, :article_id], unique: true +``` + +Once you have this unique index, attempting to add the article to a person twice +will raise an `ActiveRecord::RecordNotUnique` error: + +```ruby +person = Person.create(name: 'Honda') +article = Article.create(name: 'a1') +person.articles << article +person.articles << article # => ActiveRecord::RecordNotUnique ``` Note that checking for uniqueness using something like `include?` is subject |