Skip to content

Commit

Permalink
Readme tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mariovisic committed Jul 10, 2011
1 parent 2e8d278 commit 8760ad0
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ attachment type that correctly responds to a `path` method.
Add has_face to your Gemfile and then bundle

``` ruby
gem 'has_face'
gem 'has_face'
```

Once installed run the generator to create an initializer

``` ruby
rails g has_face:install
rails g has_face:install
```

Then open up `config/initializers/has_face.rb` and enter your face.com
API details.

``` ruby
# config/initializers/has_face.rb
HasFace.configure do |config|
config.api_key = 'your face.com API key'
config.api_secret = 'your face.com API secret'
config.skip_validation_on_error = false
end
HasFace.configure do |config|
config.api_key = 'your face.com API key'
config.api_secret = 'your face.com API secret'
config.skip_validation_on_error = false
end
```

## Usage

Simply add a validation to the image you want to ensure has faces:

``` ruby
class User < ActiveRecord::Base
validates :avatar, :has_face => true
end
class User < ActiveRecord::Base
validates :avatar, :has_face => true
end
```

The `allow_nil` and `allow_blank` options are supported:

``` ruby
class User < ActiveRecord::Base
validates :avatar, :has_face => true, :allow_blank => true
end
class User < ActiveRecord::Base
validates :avatar, :has_face => true, :allow_blank => true
end
```

## i18n
Expand All @@ -58,11 +58,11 @@ Error messages generated are i18n safe. To alter the error message shown
add this to your `config/locale/en.yml`

``` ruby
en:
activerecord:
errors:
messages:
no_face: "We couldn't see a face in your photo, try taking another one."
en:
activerecord:
errors:
messages:
no_face: "We couldn't see a face in your photo, try taking another one."
```


Expand All @@ -73,20 +73,20 @@ By default has_face will raise either a `HasFace::FaceAPIError` or
errors and then take the appropriate action in your application like so:

``` ruby
begin
@user = User.create(params[:user])
rescue HasFace::FaceAPIError, HasFace::HTTPRequestError => e
# Perform some sort of action.
end
begin
@user = User.create(params[:user])
rescue HasFace::FaceAPIError, HasFace::HTTPRequestError => e
# Perform some sort of action.
end
```

If you would like to skip valdiation when a HTTP or API error occurs
then simply turn on the `skip_validation_on_error` configuration option:

``` ruby
HasFace.configure do |config|
config.skip_validation_on_error = true
end
HasFace.configure do |config|
config.skip_validation_on_error = true
end
```

## Testing has_face
Expand All @@ -96,29 +96,31 @@ To speed up your test suite, you can disable face validations by setting the
your test config.

``` ruby
HasFace.enable_validation = false
HasFace.enable_validation = false
```

Has Face supplies a matcher which you can use in your tests, to
enable it, include the matchers module in your rspec config.

``` ruby
RSpec.configure do |config|
config.include HasFace::Test::Matchers
end
```


Once included he matcher can be used:

``` ruby
context 'validations' do
it { should validate_has_face_for :avatar }
end
context 'validations' do
it { should validate_has_face_for :avatar }
end
```

The options `allow_blank` and `allow_nil` can also be passed to the matcher:

``` ruby
it { should validate_has_face_for :avatar, :allow_blank => true }
it { should validate_has_face_for :avatar, :allow_blank => true }
```

### Contributing
Expand Down

0 comments on commit 8760ad0

Please sign in to comment.