Class: VoteFu::StarRatingComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/vote_fu/star_rating_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(voteable:, voter: nil, scope: nil, max_stars: 5, filled_star: "★", empty_star: "☆", half_star: "⯨", show_average: true, show_count: true, readonly: false) ⇒ StarRatingComponent

Star rating component for 1-5 star ratings

Uses vote values 1-5 to represent star ratings.

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • voter (ActiveRecord::Base, nil) (defaults to: nil)

    The current voter

  • scope (String, Symbol, nil) (defaults to: nil)

    Vote scope

  • max_stars (Integer) (defaults to: 5)

    Maximum number of stars (default: 5)

  • filled_star (String) (defaults to: "★")

    Character for filled star

  • empty_star (String) (defaults to: "☆")

    Character for empty star

  • half_star (String) (defaults to: "⯨")

    Character for half star (used in average display)

  • show_average (Boolean) (defaults to: true)

    Show average rating

  • show_count (Boolean) (defaults to: true)

    Show vote count

  • readonly (Boolean) (defaults to: false)

    Disable voting (show average only)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/vote_fu/star_rating_component.rb', line 19

def initialize(
  voteable:,
  voter: nil,
  scope: nil,
  max_stars: 5,
  filled_star: "",
  empty_star: "",
  half_star: "",
  show_average: true,
  show_count: true,
  readonly: false
)
  @voteable = voteable
  @voter = voter
  @scope = scope
  @max_stars = max_stars
  @filled_star = filled_star
  @empty_star = empty_star
  @half_star = half_star
  @show_average = show_average
  @show_count = show_count
  @readonly = readonly
end

Instance Method Details

#callObject



43
44
45
46
47
48
49
50
# File 'app/components/vote_fu/star_rating_component.rb', line 43

def call
  tag.div(**wrapper_attributes) do
    safe_join([
      star_container,
      (rating_info if @show_average || @show_count)
    ].compact)
  end
end