Module: VoteFu::VotesHelper

Defined in:
app/helpers/vote_fu/votes_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_vote_direction(voteable, options = {}) ⇒ Symbol?

Returns the current vote direction for a voter on a voteable

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash

Returns:

  • (Symbol, nil)

    :up, :down, or nil if not voted



157
158
159
160
161
162
163
164
# File 'app/helpers/vote_fu/votes_helper.rb', line 157

def current_vote_direction(voteable, options = {})
  voter = options.fetch(:voter) { default_voter }
  return nil unless voter

  scope = options[:scope]
  vote = voteable.received_votes.find_by(voter: voter, scope: scope)
  vote&.direction
end

#downvote_button(voteable, options = {}) ⇒ Object

Renders a standalone downvote button

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash



111
112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/vote_fu/votes_helper.rb', line 111

def downvote_button(voteable, options = {})
  voter = options.fetch(:voter) { default_voter }

  render partial: "vote_fu/votes/downvote_button", locals: {
    voteable: voteable,
    voter: voter,
    scope: options[:scope],
    label: options.fetch(:label, ""),
    class: options[:class]
  }
end

#like_button(voteable, options = {}) ⇒ Object

Renders a like button (upvote-only, social media style)

Examples:

Basic usage

<%= like_button @photo %>

With custom labels

<%= like_button @photo, liked_label: "❤️", unliked_label: "🤍" %>

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :voter (Object)

    The current voter

  • :scope (String, Symbol)

    Vote scope

  • :liked_label (String)

    Label when liked (default: "♥")

  • :unliked_label (String)

    Label when not liked (default: "♡")

  • :show_count (Boolean)

    Show like count (default: true)

  • :class (String)

    Additional CSS classes



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/vote_fu/votes_helper.rb', line 75

def like_button(voteable, options = {})
  voter = options.fetch(:voter) { default_voter }

  render partial: "vote_fu/votes/like_button", locals: {
    voteable: voteable,
    voter: voter,
    scope: options[:scope],
    liked_label: options.fetch(:liked_label, ""),
    unliked_label: options.fetch(:unliked_label, ""),
    show_count: options.fetch(:show_count, true),
    class: options[:class]
  }
end

#upvote_button(voteable, options = {}) ⇒ Object

Renders a standalone upvote button

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash



94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/vote_fu/votes_helper.rb', line 94

def upvote_button(voteable, options = {})
  voter = options.fetch(:voter) { default_voter }

  render partial: "vote_fu/votes/upvote_button", locals: {
    voteable: voteable,
    voter: voter,
    scope: options[:scope],
    label: options.fetch(:label, ""),
    class: options[:class]
  }
end

#vote_count(voteable, options = {}) ⇒ Object

Renders the vote count for a voteable

Examples:

Basic usage

<%= vote_count @post %>

With format

<%= vote_count @post, format: :percentage %>

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :scope (String, Symbol)

    Vote scope

  • :format (Symbol)

    Display format (:plusminus, :total, :percentage, :split)



50
51
52
53
54
55
56
# File 'app/helpers/vote_fu/votes_helper.rb', line 50

def vote_count(voteable, options = {})
  render partial: "vote_fu/votes/count", locals: {
    voteable: voteable,
    scope: options[:scope],
    format: options.fetch(:format, :plusminus)
  }
end

#vote_dom_id(voteable, suffix = :widget, scope: nil) ⇒ String

Returns the DOM ID for a voteable's widget

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • suffix (Symbol, String) (defaults to: :widget)

    ID suffix (:widget, :count, :error)

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

    Vote scope

Returns:

  • (String)

    The DOM ID



130
131
132
133
# File 'app/helpers/vote_fu/votes_helper.rb', line 130

def vote_dom_id(voteable, suffix = :widget, scope: nil)
  scope_part = scope.present? ? "_#{scope}" : ""
  "vote_fu_#{voteable.model_name.singular}_#{voteable.id}#{scope_part}_#{suffix}"
end

#vote_widget(voteable, options = {}) ⇒ Object

Renders the full vote widget with upvote/downvote buttons and count

Examples:

Basic usage

<%= vote_widget @post %>

With scope

<%= vote_widget @post, scope: :quality %>

Custom labels

<%= vote_widget @post, upvote_label: "👍", downvote_label: "👎" %>

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :voter (Object)

    The current voter (defaults to current_user if available)

  • :scope (String, Symbol)

    Vote scope for scoped voting

  • :show_count (Boolean)

    Whether to show vote count (default: true)

  • :upvote_label (String)

    Custom upvote button label (default: "▲")

  • :downvote_label (String)

    Custom downvote button label (default: "▼")



24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/vote_fu/votes_helper.rb', line 24

def vote_widget(voteable, options = {})
  voter = options.fetch(:voter) { default_voter }

  render partial: "vote_fu/votes/widget", locals: {
    voteable: voteable,
    voter: voter,
    scope: options[:scope],
    show_count: options.fetch(:show_count, true),
    upvote_label: options.fetch(:upvote_label, ""),
    downvote_label: options.fetch(:downvote_label, "")
  }
end

#voted_on?(voteable, options = {}) ⇒ Boolean

Checks if the current voter has voted on the voteable

Parameters:

  • voteable (ActiveRecord::Base)

    The voteable record

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :voter (Object)

    The voter to check

  • :direction (Symbol)

    :up or :down to check specific direction

  • :scope (String, Symbol)

    Vote scope

Returns:

  • (Boolean)


144
145
146
147
148
149
# File 'app/helpers/vote_fu/votes_helper.rb', line 144

def voted_on?(voteable, options = {})
  voter = options.fetch(:voter) { default_voter }
  return false unless voter

  voteable.voted_by?(voter, direction: options[:direction], scope: options[:scope])
end