LookupContext js/html fallback is hacked in there #10525

Closed
wants to merge 101 commits into
from

Conversation

Projects
None yet

ActionView::LookupContext is hardcoded to append the :html format whenever :js is set as the format. A user may want to extend this behavior to their own custom defined formats (for example a :mobile format could fallback to :html as well).

This current behavior is the magic behind allowing you to render an html partial within a javascript response.

# in new.js.erb
# will render the _form.html.erb partial
render partial: 'form', locals: {object: @object}

This change allows you to specify your own fallbacks easily. In somewhere like config/initializers/mime_types.rb:

MimeTypes.register_alias "text/html", :mobile
ActionView::LookupContext.format_fallbacks[:mobile] = [:html]

Now, if you have a request where the format is :mobile, it will try to render a mobile template first, and fallback to the html template. Additionally, you could render an html partial within a mobile template.

This behavior is already being tested in actionpack/test/template/lookup_context_test.rb.

Jeff Ching all you to add additional mime type fallbacks similar to the :js => :…
…html fallback when setting a lookup context's format
f9ceee2

@josevalim josevalim and 1 other commented on an outdated diff May 8, 2013

actionpack/lib/action_view/lookup_context.rb
def formats=(values)
if values
values.concat(default_formats) if values.delete "*/*"
- if values == [:js]
- values << :html
- @html_fallback_for_js = true
+ self.class.format_fallbacks.each do |k, v|
+ if values == [k]
+ values += v
@josevalim

josevalim May 8, 2013

Member

values.concat(v)

@chingor13

chingor13 May 8, 2013

I'll make this change

@josevalim josevalim and 1 other commented on an outdated diff May 8, 2013

actionpack/lib/action_view/renderer/abstract_renderer.rb
@@ -40,7 +40,7 @@ def instrument(name, options={})
def prepend_formats(formats)
formats = Array(formats)
- return if formats.empty? || @lookup_context.html_fallback_for_js
+ return if formats.empty? || @lookup_context.format_fallback
@josevalim

josevalim May 8, 2013

Member

@lookup_context.format_fallback will always return true now, no?

@chingor13

chingor13 May 8, 2013

format_fallback is set to true on the instance if any of the fallbacks were used just like html_fallback_for_js was set if the fallback was used.

Member

josevalim commented May 8, 2013

Thank you for the pull request, I have added some notes!

Jeff Ching and others added some commits May 8, 2013

Jeff Ching change += to concat for code consistency 7c92046
@jcxplorer jcxplorer Fix migrations with enable_extension
When using ActiveRecord::Base.table_name_prefix and/or
table_name_suffix, extension names got the same treatment as table names
when running migrations. This led to migrations that tried to call, for
example, enable_extension("prefix_hstore") on the connection.
40708c3
@sakuro sakuro Hash#deep_*_keys(!) recurse into nested arrays.
Following methods now recursively transform nested arrays, too.

* Hash#deep_transform_keys
* Hash#deep_transform_keys!
* Hash#deep_stringify_keys
* Hash#deep_stringify_keys!
* Hash#deep_symbolize_keys
* Hash#deep_symbolize_keys!
ac0b1d8
@tomykaira tomykaira Check authentication scheme in Basic auth
`authenticate_with_http_basic` and its families should check the authentication
schema is "Basic".

Different schema, such as OAuth2 Bearer should be rejected by basic auth, but
it was passing as the test shows.

This fixes #10257.
a7a377f
@tomykaira tomykaira Run login_procedure only when the auth_scheme is valid 15a98a8
@jefflai2 jefflai2 Fixes Issue #13466.
Changed the call to a scope block to be evaluated with instance_eval.
The result is that ScopeRegistry can use the actual class instead of base_class when
caching scopes so queries made by classes with a common ancestor won't leak scopes.
9c3afdc
Contributor

gregmolnar commented May 2, 2014

@josevalim Any thoughts on this? Merge or close?

@rafaelfranca rafaelfranca and 1 other commented on an outdated diff May 17, 2014

actionpack/lib/action_view/lookup_context.rb
@@ -103,7 +103,7 @@ def _set_detail(key, value)
# Helpers related to template lookup using the lookup context information.
module ViewPaths
- attr_reader :view_paths, :html_fallback_for_js
@rafaelfranca

rafaelfranca May 17, 2014

Owner

I think we should keep html_fallback_for_js deprecating it and making it change format_fallback. Could you update it? I'll merge after that.

@chingor13

chingor13 May 19, 2014

Sure, I'll do it soon.

rafaelfranca self-assigned this May 17, 2014

mikegehard and others added some commits May 17, 2014

@mikegehard mikegehard Put attr_reader in with all of the other instance methods
This makes the grouping make a little more sense
41f7d07
@sgrif sgrif PostgreSQL timestamps are always datetimes
The current behavior is that they are treated as `datetime` normally,
but if they are part of an array, they are treated as `timestamp`. The
only place that seems to be impacted by this is schema dumping, which
shouldn't matter since `t.datetime` and `t.timestamp` are equivalent in
the `PostgreSQL` adapter, anyway.
d070a65
@sgrif sgrif Use destructured arguments when looping through pairs
Minor refactoring of looping behavior for aggregation
113f56d
@dskang dskang Default config.assets.digests to true in development f369bcf
@schuetzm schuetzm Make `:index` in migrations work with all column types 9a0d35e
@eileencodes eileencodes early return on delete and destroy methods
When delete or destroy is called on all records nothing
is deleted or destroyed. Intead of running through the code and still
not deleteing anything, we should early return
540262a
@mikegehard mikegehard Let others know why this code is here
[ci skip]
8dd801d
@Gaurav2728 Gaurav2728 remove ecosystem link that is broken 67de652
@senny senny Merge pull request #15169 from Gaurav2728/gaurav-ecosystem
remove ecosystem link that is broken [ci skip]
4d8c6c5
@senny senny Merge pull request #15157 from msgehard/remove_cruft_in_has_secure_pa…
…ssword

Remove cruft now that strong_parameters is default [ci skip]
1963aa3
@vijaydev vijaydev copy edits[ci skip] ddf32da
@robin850 robin850 A tiny pass through the PostgreSQL guide [ci skip] 623da13
@tenderlove tenderlove Feature detect based on Ruby version.
I didn't want to do this, FNM_EXTGLOB is defined on 2.1.x, but Dir.glob
returns the wrong value on Ruby less than 2.2.0.  Checking for a
case-insensitive FS seems too hard, so just check Ruby version  Checking
for a case-insensitive FS seems too hard, so just check Ruby version.
239f560
@senny senny Merge pull request #15156 from sgrif/sg-postgres-timestamps
PostgreSQL timestamps should always be datetimes
d29074a
@senny senny pg guide, move introductory sentences into main content. [ci skip]
This is a reaction to 7ca75f3#commitcomment-6303828
e153fbf
@senny senny Merge pull request #14962 from arunagw/aa-fix-rake-activerecord
Reorganize ActiveRecord tasks [Arun Agrawal & Abd ar-Rahman Hamidi]
c6ee495
@senny senny pg, add missing nodocs for extracted modules. 881cab4
@senny senny pg, re-introduce `PostgreSQL::Utils` to unify schema/table extraction.
Partial revert of c0bfc3f
d4cec31
@rafaelfranca rafaelfranca Merge pull request #15160 from sgrif/sg-destructured-each
Use destructured arguments when looping through pairs
85b78fc
@rafaelfranca rafaelfranca Merge pull request #14126 from schuetzm/index_option_for_column
Make `:index` in migrations work with all column types
88ee30a
@rafaelfranca rafaelfranca Merge pull request #15168 from eileencodes/return-early-on-delete-and…
…-destroy-methods

Return early on delete and destroy methods
b52e639
@senny senny test, inline `DeveloperWithAggregate`, which is used by a single test. a0eec57
@sgrif sgrif Delegate `Column#type` to the injected type object
The decision to wrap type registrations in a proc was made for two
reasons.

1. Some cases need to make an additional decision based on the type
  (e.g. a `Decimal` with a 0 scale)
2. Aliased types are automatically updated if they type they point to is
  updated later. If a user or another adapter decides to change the
  object used for `decimal` columns, `numeric`, and `number` will
  automatically point to the new type, without having to track what
  types are aliased explicitly.

Everything else here should be pretty straightforward. PostgreSQL ranges
had to change slightly, since the `simplified_type` method is gone.
0b682e4
@senny senny Merge pull request #15158 from sgrif/sg-delegate-type
Delegate `Column#type` to the injected type object
4273554
@rafaelfranca rafaelfranca Merge pull request #10798 from jcxplorer/fix-enable_extension-with-ta…
…ble_name_prefix

Fix migrations that use enable_extension with table_name_prefix/suffix

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/migration.rb
889f61e
@rafaelfranca rafaelfranca Fixing test order issues 361d2ff
@rafaelfranca rafaelfranca Merge pull request #15155 from dskang/digest
Default config.assets.digests to true in development
09cc922
@zuhao zuhao Add ActionController::Renderers.remove. ac36b45
@sgrif sgrif Remove :timestamp column type
The `:timestamp` type for columns is unused. All database adapters treat
them as the same database type. All code in `ActiveRecord` which changes
its behavior based on the column's type acts the same in both cases.
However, when the type is passed to code that checks for the `:datetime`
type, but not `:timestamp` (such as XML serialization), the result is
unexpected behavior.

Existing schema definitions will continue to work, and the `timestamp`
type is transparently aliased to `datetime`.
d0f8c46
@rafaelfranca rafaelfranca Merge pull request #15184 from sgrif/sg-remove-timestamp-type
Remove :timestamp column type
03035d6
@tenderlove tenderlove Revert "Rewrite journey routes formatter for performance"
This reverts commit 5c224de.

Conflicts:
	actionpack/lib/action_dispatch/journey/visitors.rb

5c224de introduced a bug in the
formatter.  This commit includes a regression test.
62d1b33
@tenderlove tenderlove fix escaping in generation dc2e3ea
@tenderlove tenderlove make the AST go from left to right, rather than right to left fe06e9a
@senny senny Merge pull request #15178 from zuhao/refactor_actionpack_respond_with…
…_test

Deregister csv renderer after test to prevent leak.
00d1b13
@zuhao zuhao Add using_resouce_with_json to controller. f044020
@senny senny Merge pull request #15182 from zuhao/refactor_actionpack_respond_with…
…_test_2

Un-define :to_json for Customer class after stubbing.
7591e8c
@senny senny docs, `instantiate` expects `String` keys. [Rafal Piekarski & Yves Senn]
Closes #15122
Closes #15107
60bd243
@senny senny fix `rake test_sqlite3_mem`.
While running Sqlite3 memory tests I encountered the following error:

```
Finished in 69.416366s, 58.0267 runs/s, 162.3681 assertions/s.

  1) Error:
ActiveRecord::Migration::ChangeSchemaTest#test_add_column_with_timestamp_type:
NoMethodError: undefined method `type' for nil:NilClass
    /Users/senny/Projects/rails/activerecord/test/cases/migration/change_schema_test.rb:244:in `test_add_column_with_timestamp_type'

4028 runs, 11271 assertions, 0 failures, 1 errors, 1 skips
```

This was because the table `testings` was used in multiple test-cases.
This resulted in a wrongly cached schema on `ActiveRecord::Base.schema_chae`.

/cc @zuhao
8f8dfa4
@simi simi Use generated binstubs in guides examples.
[ci skip]
981dda5
@senny senny Merge pull request #15192 from simi/guides-binstubs
Use generated binstubs in guides examples. [ci skip]
528aff1
@camsong camsong Distinguish rake assets:clobber from rake assets:clean 9aa807c
@eric-chahin @matthewd eric-chahin Fixed the inferred table name for HABTM within a schema
Fixes #14824.
1f4daf1
@matthewd matthewd Merge pull request #14965 from eric-chahin/issue_14824
Fixed the inferred table name of a HABTM auxiliar
f9860fc
@sgrif sgrif Delegate `#type_cast` to injected type objects on SQLite3 36fde2b
@senny senny Merge pull request #15197 from sgrif/sg-delegate-type-cast-sqlite3
Delegate `#type_cast` to injected type objects on SQLite3
d17b056
@v-yarotsky v-yarotsky Fix confusing exception in ActiveSupport delegation 6cc5a86
@sgrif sgrif Delegate type_cast to injected type object in mysql ac37165
@sgrif sgrif Have Postgres OID types inherit from general types
Using general types where possible. Several more can go away once
infinity gets figured out.
b2242d1
@senny senny Merge pull request #15198 from sgrif/sg-delegate-type-cast-mysql
Delegate type_cast to injected type object in mysql
89ca680
@sgrif sgrif Use general types for mysql fields ca703bb
@sgrif sgrif Replace `type_cast` case statement with delegation
All subclasses of column were now delegating `type_cast` to their
injected type object. We can remove the overriding methods, and
generalize it on the `Column` class itself. This also enabled us to
remove several column classes completely, as they no longer had any
meaningful behavior of their own.
e781aa3
@rafaelfranca rafaelfranca Merge pull request #15199 from sgrif/sg-types-mysql
Use general types for mysql fields
9d835fd
@tenderlove tenderlove fix multiple hash preloads. Fixes #14994 b713e20
@rafaelfranca rafaelfranca Merge pull request #15201 from sgrif/sg-types-postgresql
Have Postgres OID types inherit from general types
4f41646
@tenderlove tenderlove make the each visitor top-down left-right e086964
@tenderlove tenderlove prepopulate the dispatch cache so we don't need the ThreadSafe cache. 74a8477
@rafaelfranca rafaelfranca Merge pull request #15187 from v-yarotsky/fix_confusing_delegation_ex…
…ception

Fix confusing exception in ActiveSupport delegation
88d08f2
@rafaelfranca rafaelfranca Merge pull request #15203 from sgrif/sg-delegate-type-cast
Replace `type_cast` case statement with delegation
59ee23f
@senny senny Merge pull request #15191 from camsong/master
Distinguish rake assets:clobber from rake assets:clean

[ci skip]
f3407d1
@sgrif sgrif Use the generic type map object for mysql field lookups ac4c1c3
@rafaelfranca rafaelfranca Merge pull request #15200 from sgrif/sg-type-map-mysql
Use the generic type map object for mysql field lookups
5300844
@tenderlove tenderlove translate AST to a formatter before url generation e883db0
@tenderlove tenderlove cache the formatter on the path object c99ff6d
@tenderlove tenderlove remove dead code 25c6726
@sgrif sgrif Delegate predicate methods to injected type object on Column 50869f3
@sgrif sgrif Use the generic type map for PostgreSQL OID registrations 9f61f31
@rafaelfranca rafaelfranca Merge pull request #15204 from sgrif/sg-delegate-predicates
Delegate predicate methods to injected type object on Column
c4640ca
@rafaelfranca rafaelfranca Merge pull request #15206 from sgrif/sg-type-map-postgresql
Use the generic type map for PostgreSQL OID registrations
90a82cb
@sgrif sgrif Inline typecasting helpers from Column to the appropriate types 8f38799
@rafaelfranca rafaelfranca Merge pull request #15207 from sgrif/sg-inline-column-helpers
Inline typecasting helpers from Column to the appropriate types
52b5586
@rafaelfranca rafaelfranca Merge pull request #11346 from tomykaira/fix_10257
Check authentication scheme in Basic auth
ef00bb7
@sgrif sgrif Delegate `type_cast_for_write` to injected type object 32218c1
@rafaelfranca rafaelfranca Merge pull request #15208 from sgrif/sg-delegate-type-cast-for-write
Delegate `type_cast_for_write` to injected type object
470c4df
@tenderlove tenderlove fix variable names, only pass hashes to the positional args method 84bf3a0
@tenderlove tenderlove fewer hash allocations when calling url_for 5e181ed

I'm still seeing the behavior in #14994 with this patch. Somehow, in my application, one of the recs is a nil value (rather than empty?EDIT: [nil], not nil)... adding a compact to line 119 fixes my issues.

still trying to wrap my head around what this code is exactly trying to do, and trying to create a test case to help.

Any guidance would be much appreciated.

THANK YOU SO MUCH <33333333

Owner

tenderlove replied May 20, 2014

Is it possible to make a test case to reproduce the error?

ah, i think it when you include a has_one, plus some dependent records, which would explain why the recs could be nil and not an empty array

@tenderlove trying to now

tenderlove and others added some commits May 20, 2014

@tenderlove tenderlove fewer method calls and string garbage when there is no user/password 6004c75
@tenderlove tenderlove push arg checking up 960398c
@tenderlove tenderlove push only_path conditional up 5f49da8
@tenderlove tenderlove mutate the path string to avoid object allocations b610104
@tenderlove tenderlove fewer string allocations per url_for ba487b9
@tenderlove tenderlove we don't use this parameter for anything, so rm 089d9ba
@rafaelfranca rafaelfranca Merge pull request #15154 from msgehard/move_password_field
Put attr_reader in with all of the other instance methods
5508a3e
@brocktimus brocktimus Prevented belongs_to: touch propagating up if there are no changes be…
…ing saved
713fc39
@sgrif sgrif Delegate `klass` to the injected type object c4245c3
@rafaelfranca rafaelfranca Revert "Revert "Merge pull request #8313 from alan/only_save_changed_…
…has_one_objects""

This reverts commit e94e6c2.

Conflicts:
	activerecord/CHANGELOG.md

The original commit was reverted only to be safe since #14407 were reported.
We don't have any proof we added a regression with the original commit
so reverting it now will give us more problem.

Closes #14407
26931eb
@rafaelfranca rafaelfranca Merge pull request #14979 from brocktimus/master
Making belongs_to: touch behaviour be consistent with save updating updated_at
437f464
@rafaelfranca rafaelfranca Merge pull request #15205 from sgrif/sg-delegate-klass
Delegate `klass` to the injected type object
c72d6c9
@rafaelfranca rafaelfranca Merge pull request #14544 from jefflai2/named_scope_sti
Fixes Issue #13466.

Conflicts:
	activerecord/CHANGELOG.md
9a1abed
@rafaelfranca rafaelfranca Merge pull request #10887 from sakuro/deep_transform_keys_in_nested_a…
…rrays

Hash#deep_*_keys(!) recurse into nested arrays.

Conflicts:
	activesupport/CHANGELOG.md
decd719
@rafaelfranca rafaelfranca Merge pull request #15213 from tgxworld/remove_redundant_code
Remove redundant code.
b17f6e6
Jeff Ching all you to add additional mime type fallbacks similar to the :js => :…
…html fallback when setting a lookup context's format
323cf79
Jeff Ching change += to concat for code consistency abb5cc6
Jeff Ching Merge branch 'master' of github.com:chingor13/rails 9ada33a

Shoot, I messed up the merge from rails master.

Since I messed up this merge, I made a new pull request #15224

chingor13 closed this May 21, 2014

Member

schneems commented on f369bcf Jun 4, 2014

❤️ this so much. Thanks!

Owner

rafaelfranca replied Aug 20, 2014

Right

Rational#to_d in Ruby 2.1.5 requires a precision parameter to be specified.

My client's auth scheme was not valid. :'(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment