lib/is_reviewable/reviewable.rb
# View the object with and identifier (user or ip) - create new if new reviewer.
#
# === identifiers_and_options hash:
# * <tt>:reviewer/:user/:account</tt> - identify with a reviewer-model or IP (e.g. User, Account, ..., "128.0.0.1")
# * <tt>:rating</tt> - Review rating value, e.g. 3.5, "3.5", ... (optional)
# * <tt>:body</tt> - Review text body, e.g. "Lorem *ipsum*..." (optional)
# * <tt>:*</tt> - Any custom review field, e.g. :reviewer_mood => "angry" (optional)
#
View the object with and identifier (user or ip) - create new if new reviewer.
View the object with and identifier ( user or ip ) - create new if new reviewer .
def review!(identifiers_and_options)
        begin
          reviewer = self.validate_reviewer(identifiers_and_options)
          review = self.review_by(identifiers_and_options)
          review_values = identifiers_and_options.except(*::IsReviewable::Review::ASSOCIATIVE_FIELDS)
          review_values[:rating] = review_values[:rating].to_f if review_values[:rating].present?
          if review_values[:rating].present? && !self.valid_rating_value?(review_values[:rating])
            raise InvalidReviewValueError, "Invalid rating value: #{review_values[:rating]} not in [#{self.rating_scale.join(', ')}]."
          end
          unless review.present?
            review = ::Review.new do |r|
              r.reviewable_id   = self.id
              r.reviewable_type = self.class.name
              if Support.is_active_record?(reviewer)
                r.reviewer_id   = reviewer.id
                r.reviewer_type = reviewer.class.name
              else
                r.ip = reviewer
              end
            end
            self.reviews << review
          else
          end
          review.attributes = review_values.slice(*review.attribute_names.collect { |an| an.to_sym })
          review.save && self.update_cache!
          review
        rescue InvalidReviewerError, InvalidReviewValueError => e
          raise e
        rescue Exception => e
          raise RecordError, "Could not create/update review #{review.inspect} by #{reviewer.inspect}: #{e}"
        end
      end
def review! ( identifiers_and_options ) begin reviewer = self . validate_reviewer ( identifiers_and_options ) review = self . review_by ( identifiers_and_options ) review_values = identifiers_and_options . except ( * :: IsReviewable :: Review :: ASSOCIATIVE_FIELDS ) review_values [ :rating ] = review_values [ :rating ] . to_f if review_values [ :rating ] . present? if review_values [ :rating ] . present? && ! self . valid_rating_value? ( review_values [ :rating ] ) raise InvalidReviewValueError , "Invalid rating value: #{review_values[:rating]} not in [#{self.rating_scale.join(', ')}]." end unless review . present? review = :: Review . new do | r | r . reviewable_id = self . id r . reviewable_type = self . class . name if Support . is_active_record? ( reviewer ) r . reviewer_id = reviewer . id r . reviewer_type = reviewer . class . name else r . ip = reviewer end end self . reviews << review else end review . attributes = review_values . slice ( * review . attribute_names . collect { | an | an . to_sym } ) review . save && self . update_cache! review rescue InvalidReviewerError , InvalidReviewValueError => e raise e rescue Exception => e raise RecordError , "Could not create/update review #{review.inspect} by #{reviewer.inspect}: #{e}" end end
View the object with and identifier (user or ip) - create new if new reviewer.
View the object with and identifier ( user or ip ) - create new if new reviewer .
# Except for the reserved fields, any Review-fields should be be able to update. # An un-existing reviewer of this reviewable object => Create a new review. # An existing reviewer of this reviewable object => Update the existing review. # Update non-association attributes, such as rating, body (the review text), and any custom fields. # Save review and cachable data.
identifiers_and_options
<not_specific>