Module: Helpers

Included in:
ActionView::Base
Defined in:
lib/mongoid/letsrate/helpers.rb

Instance Method Summary collapse

Instance Method Details

#rating_for(rateable_obj, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mongoid/letsrate/helpers.rb', line 3

def rating_for(rateable_obj, options={})

  cached_average = rateable_obj.average

  avg = cached_average ? cached_average.avg : 0

  star = options[:star] || 5

  disable_after_rate = options[:disable_after_rate].nil? ? true : options[:disable_after_rate]

  readonly = !(current_user && rateable_obj.can_rate?(current_user))

   :div, '', :class => "star", "data-rating" => avg,
              "data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name,
              "data-disable-after-rate" => disable_after_rate,
              "data-readonly" => readonly,
              "data-star-count" => star
end

#rating_for_user(rateable_obj, rating_user, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid/letsrate/helpers.rb', line 22

def rating_for_user(rateable_obj, rating_user, options = {})
  @object = rateable_obj
  @user = rating_user
  @rating = Rate.find_by_rater_id_and_rateable_id(@user.id, @object.id)
  stars = @rating ? @rating.stars : 0

  disable_after_rate = options[:disable_after_rate].nil? ? true : options[:disable_after_rate]

  readonly=false
  if disable_after_rate
    readonly = current_user.present? ? !rateable_obj.can_rate?(current_user.id) : true
  end

   :div, '', :class => "star", "data-rating" => stars,
              "data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name,
              "data-disable-after-rate" => disable_after_rate,
              "data-readonly" => readonly,
              "data-star-count" => stars
end