<commit_before>module IsRateable
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  module ClassMethods
    def is_rateable(options={})
      include InstanceMethods
      has_many :ratings, :as => :rateable
      
      cattr_accessor :maximum_rating_allowed
      self.maximum_rating_allowed = options[:upto] || 5
      
      ActionController::Routing::Routes.add_route('/:controller/:id/rate/:rating', :action => 'rate', :method => :put)
    end
  end

  module InstanceMethods
    def rating
      if (rating = self.ratings.average(:value))
        rating.round
      else
        0
      end
    end
    
    def rating_range
      1..self.maximum_rating_allowed
    end
    
    def add_rating(value, options={})
      self.ratings.create({ :value => value }.merge(options))
    end
    
    def rating_in_words
      case self.rating
      when 0
        "no"
      when 1
        "one"
      when 2
        "two"
      when 3
        "three"
      when 4
        "four"
      when 5
        "five"
      end      
    end
  end
  
  module ViewMethods
    # polymorphic url sucks big time. unfortunately I ended up having to create a method to do this.
    def rating_url(record, value)
      url_for :controller => record.class.to_s.downcase.pluralize, :id => record.to_param, :action => "rate", :rating => value, :method => :put
    end

    def render_rating(record, type=:simple, units="star")

      case type
      when :simple
        "#{record.rating}/#{record.maximum_rating_allowed} #{pluralize(record.rating, units)}"
      when :interactive_stars
        content_tag(:ul, :class =>  "rating #{record.rating_in_words}star") do
          (1..maximum_rating_allowed).map do |i|
            content_tag(:li, content_tag(:a, i.to_s, :href => rating_url(record, 1), :title => "Rate this #{i.to_s} #{pluralize(i, units)} out of #{record.maximum_rating_allowed}"), :class => "rating-#{i}")
        end.join("\n")
      end
    end
  end
end<commit_msg>Remove magic number 1 and replace with minimum_rating_allowed<commit_after>module IsRateable
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  module ClassMethods
    def is_rateable(options={})
      include InstanceMethods
      has_many :ratings, :as => :rateable
      
      cattr_accessor :maximum_rating_allowed
      self.minimum_rating_allowed = options[:from] || 0
      self.maximum_rating_allowed = options[:upto] || 5
      
      ActionController::Routing::Routes.add_route('/:controller/:id/rate/:rating', :action => 'rate', :method => :put)
    end
  end

  module InstanceMethods
    def rating
      if (rating = self.ratings.average(:value))
        rating.round
      else
        0
      end
    end
    
    def rating_range
      self.minimum_rating_allowed..self.maximum_rating_allowed
    end
    
    def add_rating(value, options={})
      self.ratings.create({ :value => value }.merge(options))
    end
    
    def rating_in_words
      case self.rating
      when 0
        "no"
      when 1
        "one"
      when 2
        "two"
      when 3
        "three"
      when 4
        "four"
      when 5
        "five"
      end      
    end
  end
  
  module ViewMethods
    # polymorphic url sucks big time. unfortunately I ended up having to create a method to do this.
    def rating_url(record, value)
      url_for :controller => record.class.to_s.downcase.pluralize, :id => record.to_param, :action => "rate", :rating => value, :method => :put
    end

    def render_rating(record, type=:simple, units="star")

      case type
      when :simple
        "#{record.rating}/#{record.maximum_rating_allowed} #{pluralize(record.rating, units)}"
      when :interactive_stars
        content_tag(:ul, :class =>  "rating #{record.rating_in_words}star") do
          (minimum_rating_allowed..maximum_rating_allowed).map do |i|
            content_tag(:li, content_tag(:a, i.to_s, :href => rating_url(record, 1), :title => "Rate this #{i.to_s} #{pluralize(i, units)} out of #{record.maximum_rating_allowed}"), :class => "rating-#{i}")
        end.join("\n")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11475808412351082, 'fraction_numerical': 0.005348893751519572, 'mean_word_length': 2.4717299578059073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2712390', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1285, 'n_words': 317}