Revert "up: removes validation to allow your custom one"

This reverts commit 763a1cd818.
main
Washington Botelho 2018-02-28 13:58:56 -03:00
parent 33507138c6
commit 30b4ac9484
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
3 changed files with 11 additions and 13 deletions

View File

@ -347,19 +347,6 @@ rating:
Now the rates will be written on `reviews` table over `rating_rates` and calculation will be on `review_ratings` over `rating_ratings`.
You can change one table o both of them.
## Validation
It is recommended that you add a validation to ensure that the author do not vote multiple times on the same resource and its scope.
This validation is not inside the gem since you could want make your own custom validation.
```ruby
validates :author_id, uniqueness: {
case_sensitive: false,
scope: %i[author_type resource_id resource_type scopeable_id scopeable_type]
}
```
## Love it!
Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=rating) or [Patreon](https://www.patreon.com/wbotelhos). Thanks! (:

View File

@ -14,6 +14,11 @@ module Rating
validates :author, :resource, :value, presence: true
validates :value, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 100 }
validates :author_id, uniqueness: {
case_sensitive: false,
scope: %i[author_type resource_id resource_type scopeable_id scopeable_type]
}
def self.create(author:, metadata:, resource:, scopeable: nil, value:)
record = find_or_initialize_by(author: author, resource: resource, scopeable: scopeable)

View File

@ -18,4 +18,10 @@ RSpec.describe Rating::Rate do
it do
is_expected.to validate_numericality_of(:value).is_less_than_or_equal_to(100).is_less_than_or_equal_to 100
end
it do
expect(object).to validate_uniqueness_of(:author_id)
.scoped_to(%i[author_type resource_id resource_type scopeable_id scopeable_type])
.case_insensitive
end
end