songkick/oauth2-provider — pull request #53 by harigopal (2013-07-09 09:15:43+00:00)
Title: Updating to remove deprecation warnings when using ActiveRecord 4.

Hi, because of deprecation warnings being raised by ActiveRecord (AR) 4, I've made a few changes which will probably neccessitate a major version bump. I'm sorry, but I couldn't think of any way to support the 2.x versions of ActiveRecord. :-(

### Deprecations warnings removed:

#### count(conditions)

```ruby
client.authorizations.count(:conditions => {:code => code}).zero?
```
...many instances of above count method were causing:

DEPRECATION WARNING: Relation#calculate with finder options is deprecated. Please build a scope and then call calculate on it instead.
DEPRECATION WARNING: The :distinct option for `Relation#count` is deprecated. Please use `Relation#distinct` instead. (eg. `relation.distinct.count`).

I replaced all instance of this count usage with:
```ruby
client.authorizations.where(:code => code).count.zero?
```

#### find_or_create_by_column(value)
```ruby
find_or_create_by_name(name)
```
caused:

DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead.

However, the suggested method works only from AR4 onwards, so I replaced it with:
```ruby
where(name: name).first || create(name: name)
```
which works for AR3.0 onwards (where(conditions).find_or_create was only added in AR3.2)

### Related changes:

I added AR4 to the Appraisals file, along with the required version of the mysql gem, and protected_attributes gem to allow mass-assignment protection in gem's model code.

### RSpec results

**AR2.x** - Failure.
**AR3.x** - Passes for SQLite and MySQL, and Postgres.
**AR4** - Passes for MySQL and Postgres. Tests don't start running for SQLite (details below).

I couldn't get AR4 specs to work with SQLite. It complains that an index name is too long:

Index name 'index_oauth2_authorizations_on_client_id_and_refresh_token_hash' on table 'oauth2_authorizations' is too long; the limit is 62 characters

Running the specs with AR4 (other DB-s) does not generate any more deprecation warnings, but the specs still have very noisy output, with AR complaining about attempts to mass-assign protected attributes (spec appears to test this). I haven't made any alterations there since its possible the specs may need to be updated (now that mass-assignment security is being moved out of the model).

I'd really appreciate it if you could take a look at the changes, and incorporate them if it makes sense. Thanks!

**Note:** I ran all tests with Ruby 2.0.0-p247.