Commit Graph

2044 Commits

Author SHA1 Message Date
Godfrey Chan
0f33d70e89 Improved compatibility with the stdlib JSON gem.
Previously, calling `::JSON.{generate,dump}` sometimes causes
unexpected failures such as intridea/multi_json#86.

`::JSON.{generate,dump}` now bypasses the ActiveSupport JSON encoder
completely and yields the same result with or without ActiveSupport.
This means that it will **not** call `as_json` and will ignore any
options that the JSON gem does not natively understand. To invoke
ActiveSupport's JSON encoder instead, use `obj.to_json(options)` or
`ActiveSupport::JSON.encode(obj, options)`.
2013-11-14 15:46:43 -08:00
KD
133399482c Array#split preserving the calling array 2013-11-11 11:32:30 +05:30
Ryan Glover
a64d4e85e3 Standardize all JSON encoded times to use 3 decimal fractional seconds 2013-11-07 10:43:51 -05:00
Godfrey Chan
798881ecd4 Do not expose internal state in the public encoder API (i.e. as_json)
See [1] for why this is not a good idea.

As part of this refactor, circular reference protection in as_json has
been removed and the corresponding error class has been deprecated.

As discussed with @jeremy, circular reference error is considered
programmer errors and protecting against it is out of scope for
the encoder.

This is again based on the excellent work by @sergiocampama in #11728.

[1]: https://github.com/intridea/multi_json/pull/138#issuecomment-24468223
2013-11-06 17:16:11 -08:00
claudiob
c61544c781 Add +capitalize+ option to Inflector.humanize
So strings can be humanized without being capitalized:

    'employee_salary'.humanize                    # => "Employee salary"
    'employee_salary'.humanize(capitalize: false) # => "employee salary"
2013-11-06 13:03:46 -08:00
Godfrey Chan
ff1192fea4 Eliminate JSON.{parse,load,generate,dump} and def to_json
JSON.{dump,generate} offered by the JSON gem is not compatiable with
Rails at the moment and can cause a lot of subtle bugs when passed
certain data structures. This changed all direct usage of the JSON gem
in internal Rails code to always go through AS::JSON.{decode,encode}.

We also shouldn't be implementing `to_json` most of the time, and
these occurances are replaced with an equivilent `as_json`
implementation to avoid problems down the road.

See [1] for all the juicy details.

[1]: intridea/multi_json#138 (comment)
2013-11-05 22:26:45 -08:00
Godfrey Chan
134c1156dd Fixed Object#as_json and Struct#as_json with options
These methods now takes the same options as Hash#as_json, for example:

    struct = Struct.new(:foo, :bar).new
    struct.foo = "hello"
    struct.bar = "world"
    json = struct.as_json(only: [:foo]) # => {foo: "hello"}

This is extracted from PR #11728 from @sergiocampama, see also the
discussion in #11460.
2013-11-05 19:22:03 -08:00
David Heinemeier Hansson
423249504a Added Numeric#in_milliseconds, like 1.hour.in_milliseconds, so we can feed them to JavaScript functions like getTime(). 2013-11-02 15:40:16 -07:00
Godfrey Chan
1fb7969154 Raise an error when AS::JSON.decode is called with options
Rails 4.1 has switched away from MultiJson, and does not currently
support any options on `ActiveSupport::JSON.decode`. Passing in
unsupported options (i.e. any non-empty options hash) will now raise
an ArgumentError.

Rationale:

1. We cannot guarantee the underlying JSON parser won't change in the
   future, hence we cannot guarantee a consistent set of options the
   method could take

2. The `json` gem, which happens to be the current JSON parser, takes
   many dangerous options that is irrelevant to the purpose of AS's
   JSON decoding API

3. To reserve the options hash for future use, e.g. overriding default
   global options like ActiveSupport.parse_json_times

This change *DOES NOT* introduce any changes in the public API. The
signature of the method is still decode(json_text, options). The
difference is this method previously accepted undocumented options
which does different things when the underlying adapter changes. It
now correctly raises an ArgumentError when it encounters options that
it does not recognize (and currently it does not support any options).
2013-10-30 10:56:00 -07:00
Jeremy Kemper
dae66a0c97 Merge pull request #12203 from chancancode/eager_load_json
Eagerload active_support/json/encoding in active_support/core_ext/object/to_json
2013-10-30 10:43:43 -07:00
Rafael Mendonça França
f18cf8e488 Merge pull request #11785 from grosser/grosser/file-unless-exist
support :unless_exist for FileCache

Conflicts:
	activesupport/CHANGELOG.md
	activesupport/test/caching_test.rb
2013-10-28 19:09:22 -02:00
Rafael Mendonça França
24b690eae6 Improve the test code for #8422
* Remove unneeded comments.
* Use key? instead of keys.include?.
2013-10-24 20:14:30 -02:00
Rafael Mendonça França
6ed5d515cf Merge pull request #8491 from igagnidz/master
Issue #8442
2013-10-24 20:11:50 -02:00
Antonio Santos
f13fce617d slice! should not remove default hash value/proc 2013-10-24 09:17:35 +02:00
Vipul A M
ec620e0984 $SAFE = 4; has been removed with Ruby 2.1
For background -
https://bugs.ruby-lang.org/issues/8468
Changset - https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/41259/diff/test/ruby/test_thread.rb
2013-10-14 21:52:25 +05:30
Jeremy Kemper
365110196a Revert "Merge pull request #12480 from iwiznia/master"
This reverts commit e5f5a838b96a362534d9bb60d02334439ed9784c, reversing
changes made to d7567f3290a50952494e9213556a1f283a6cf3a0.
2013-10-11 13:05:29 -07:00
Ionatan Wiznia
90871f7198 flatten and flatten! methods for ActiveSupport::Duration 2013-10-11 12:50:23 +02:00
Xavier Noria
2254615bff Merge pull request #12412 from bf4/allow_pathname_for_require_dependency
Allow Pathname for require dependency
2013-09-30 23:22:11 -07:00
Benjamin Fleischer
0b0beb71d6 require_dependency should allow Pathname-like objects, not just String 2013-09-30 19:57:03 -05:00
Andrew White
ffc8abd41b Merge pull request #11474 from bogdan/time-with-zone-succ
Prevent server blow up when iterating over TimeWithZone Range
2013-09-22 12:33:12 -07:00
Daniel Schierbeck
d2824a347f Allow attaching to AS::Notifications namespace up front
Before, you were required to attach *after* adding the methods to the
class, since the attachment process needed the methods to be present.

With this change, any new method will also be attached to the configured
namespace.
2013-09-20 10:14:28 +02:00
Godfrey Chan
64c88fb5d2 Moved all JSON core extensions into core_ext/object/json
TL;DR The primary driver is to remove autoload surprise.

This is related to #12106. (The root cause for that ticket is that
json/add defines Regexp#to_json among others, but here I'll reproduce
the problem without json/add.)

Before:

   >> require 'active_support/core_ext/to_json'
   => true
   >> //.as_json
   NoMethodError: undefined method `as_json' for //:Regexp
     from (irb):3
     from /Users/godfrey/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
   >> //.to_json
   => "\"(?-mix:)\""
   >> //.as_json
   => "(?-mix:)"

After:

   >> require 'active_support/core_ext/to_json'
   => true
   >> //.as_json
   => "(?-mix:)"

This is because ActiveSupport::JSON is autoloaded the first time
Object#to_json is called, which causes additional core extentions
(previously defined in active_support/json/encoding.rb) to be loaded.

When someone require 'active_support/core_ext', the expectation is
that it would add certain methods to the core classes NOW. The
previous behaviour causes additional methods to be loaded the first
time you call `to_json`, which could cause nasty surprises and other
unplesant side-effects.

This change moves all core extensions in to core_ext/json. AS::JSON is
still autoloaded on first #to_json call, but since it nolonger
include the core extensions, it should address the aforementioned bug.

*Requiring core_ext/object/to_json now causes a deprecation warnning*
2013-09-13 03:04:10 -07:00
Rafael Mendonça França
cdc10c898d Merge pull request #12196 from h-lame/fix-activesupport-cache-filestore-cleanup
Fix FileStore#cleanup to no longer rely on missing each_key method
2013-09-12 11:32:45 -07:00
Godfrey Chan
52fb1a9565 Enabled quirks mode on JSON.parse, fixes broken test in af9caae
It turns out that ActionPack depends on the decoder to parse JSON
"fragments" (e.g. '"a string"', '1', 'null', etc), so we need to
enable quirks mode on JSON.parse. Also added coverage on the decoder
side to prevent regression.
2013-09-12 09:58:18 -07:00
Godfrey Chan
b9e142af52 Replace JSON.load with JSON.parse, also removed the proc parameter
Since we are dealing with untrusted user input, we should not be
using JSON.load. According to the docs[1]:

BEWARE: This method is meant to serialise data from trusted user
input, like from your own database server or clients under your
control, it could be dangerous to allow untrusted users to pass
JSON sources into it. The default options for the parser can be
changed via the ::load_default_options method.

[1] http://www.ruby-doc.org/stdlib-2.0/libdoc/json/rdoc/JSON.html#method-i-load
2013-09-11 17:22:09 -07:00
Rafael Mendonça França
bca623bc53 Merge pull request #12200 from dchelimsky/simplify-duration-inspect-even-more
Reduce Duration#inspect to a single series of transformations
2013-09-11 14:58:46 -07:00
David Chelimsky
b16b08ebde Reduce Duration#inspect to a single series of transformations
* eliminates need for temp Hash

Also added a couple of examples to DurationTest to specify:

* duration can be defined with units out of order e.g.
  1.month + 1.year + 1.second + 1.day
* equality with a Fixnum works regardless of which operand is on which
  side of the operator
2013-09-11 15:28:38 +02:00
Murray Steele
accd4926cc Fix FileStore#cleanup to no longer rely on missing each_key method 2013-09-11 13:58:40 +01:00
Akira Matsuda
e837e5a96b Be sure to restore the default I18n.locale after changed its value in a test 2013-09-10 19:54:49 +02:00
Guillermo Iguaran
005b33222d Merge pull request #12173 from arunagw/fixes_reverted_by_revert_commit
Revert fixes
2013-09-10 07:05:17 -07:00
Anupam Choudhury
44f203b0e3 Removed unused modules and classes 2013-09-10 16:44:44 +05:30
Anupam Choudhury
f3e4069ede Removed unnecessary require 2013-09-10 13:22:30 +05:30
Arun Agrawal
6f0b1ae04e Fix order dependent test
see c48cbb02d7e6f585e31b404c0c363f0d6b586811
2013-09-09 17:11:16 +02:00
grosser
c17bd7476a support :unless_exist for FileCache 2013-08-30 12:53:35 +02:00
Simon Coffey
b4a9668626 Ensure all-caps nested consts marked as autoloaded
Previously, an autoloaded constant `HTML::SomeClass` would not be marked
as autoloaded by AS::Dependencies. This is because the
`#loadable_constants_for_path` method uses `String#camelize` on the
inferred file path, which in turn means that, unless otherwise directed,
AS::Dependencies watches for loaded constants in the `Html` namespace.

By passing the original qualified constant name to `#load_or_require`,
this inference step is avoided, and the new constant is picked up in the
correct namespace.
2013-08-27 08:20:33 +01:00
Rafael Mendonça França
7a31388682 Merge pull request #11932 from gaurish/skip-encoding-convertor
[JRuby] Skip few of the ActiveSupport's inflector test cases

Conflicts:
	activesupport/test/abstract_unit.rb
2013-08-21 14:49:03 -03:00
Gaurish Sharma
e998edd2c0 Define jruby_skip to skip test on JRuby 2013-08-21 22:59:32 +05:30
Federico Ravasio
a9f6c8f8e1 Skip tests involving $SAFE, it's not supported on Rubinius. 2013-08-21 17:07:45 +02:00
Federico Ravasio
9773d2c291 Added method to skip tests on Rubinius to AS/abstract_unit. 2013-08-21 17:07:33 +02:00
Gaurish Sharma
a67b956d28 Skip few of the ActiveSupport'sinflector test on JRuby 2013-08-21 09:26:59 +05:30
Nick Howard
dfb923e6e5 ensure freeze on Thread freezes locals 2013-08-19 12:39:40 -06:00
Vijay Dev
9abe72c760 Merge branch 'master' of github.com:rails/docrails
Conflicts:
	actionview/README.rdoc
	activerecord/lib/active_record/migration.rb
	guides/source/development_dependencies_install.md
	guides/source/getting_started.md
2013-08-17 21:49:16 +05:30
Vijay Dev
ec8ef1e105 Revert "Merge branch 'master' of github.com:rails/docrails"
This reverts commit 70d6e16fbad75b89dd1798ed697e7732b8606fa3, reversing
changes made to ea4db3bc078fb3093ecdddffdf4f2f4ff3e1e8f9.

Seems to be a code merge done by mistake.
2013-08-17 21:46:39 +05:30
David Heinemeier Hansson
5da23a3f92 Add String#remove(pattern) as a short-hand for the common pattern of String#gsub(pattern, '') 2013-08-13 12:16:25 -05:00
Xavier Noria
c7ac0a5b28 renames the :abort deprecation behaviour to :raise
That is a better name, thanks @jeremy.
2013-08-13 01:44:52 +02:00
Xavier Noria
73aad75f56 defines a new :abort deprecation behaviour that raises
See the CHANGELONG message in the patch for further details.
2013-08-13 01:27:18 +02:00
Gaurish Sharma
7191a5d830 Avoid defining multibyte method names in JSON decoding test for JRuby Compat
This change is similar to #11736 & in same way switched with fixed string & the index of the hash for method name. the index was added because otherwise, ruby will raise Error.
2013-08-07 13:19:14 +05:30
Andrew White
dfcef831b5 Fix unused variable warning
We need to call `in_time_zone` to test that it isn't modifying the receiver
but since the variable isn't used it raises a warning so add an assertion
to make Ruby think it's being used.
2013-08-04 07:09:30 +01:00
Gaurish Sharma
ab5e99ed63 Avoid calling define_method with non-english chars in InflectorTest
When we call define_method with non-english chars like ¿por qué? it
errors out on JRuby as of 1.7.4 & would leave out the following error

invalid byte sequence in US-ASCII

To work around this issue, I have switched to define_test method call
define method with fixed string & the index of the hash. the index was
added because otherwise, ruby will raise method redefined warning.

As far as I can see there are no side-effect of this change for
other implementations. For readbility I have added a message to
asssert_equal informing for which word/phase the test has passed.

Before this Change:
JRuby:
Tests terminated suddenly with an error. no reported of Failues
or errors
MRI:
All Green.

After this Change,
JRuby:
the `ActiveSupport` TestsSuite gracefully fails with report at the end which test failed & why.
MRI:
All Green(no change)
2013-08-03 18:21:17 +05:30
Gilad Zohari
3102a9aa4c Refactor Date, Time, DateTime timezone methods
Similar implementations of #in_time_zone exists for Date, Time and DateTime so
method is extracted into its own module. Also some logic is extracted into
private method.
2013-08-01 00:27:25 +03:00