Sha256: 5a8bec9c5713fff37b5f7cf86f7e75d4416b9c132f60444c0fbe7c2a657bbfab

Contents?: true

Size: 1.58 KB

Versions: 10

Compression:

Stored size: 1.58 KB

Contents

# -*- encoding : utf-8 -*-
module LoyalCore
  module ActsAsRatingTrackAble
    def self.included base
      base.class_eval do
        extend ClassMethods
      end
    end

    module ClassMethods
      def loyal_core_acts_as_rating_track_able *args
        has_many :rating_tracks, :class_name => "LoyalCore::RatingTrack", :as => :target

        include InstanceMethods
      end

      module InstanceMethods
        def user_rating_tracks user
          self.rating_tracks.filter_by_creator(user)
        end

        # 是否被**打分?
        def rating_by?(user)
          self.user_rating_tracks(user).exists?
        end

        # 加分
        def up_rating_by!(user, score=1)
          if rating_by? user
            :already
          else
            self.rating_by! user, score
            :up
          end
        end

        # 打低分
        def down_rating_by!(user, score=-1)
          if rating_by? user
            :already
          else
            self.rating_by! user, score
            :down
          end
        end

        # 用户进行打分
        def rating_by!(user, score=1)
          tmp_scope = self.user_rating_tracks(user)

          tmp_scope.first || tmp_scope.create(
            :score => score
          )

          _expire_rating_track_cache
        end

        # 取消打分
        def undo_rating_by!(user)
          self.user_rating_tracks(user).destroy_all

          _expire_rating_track_cache
        end

        private

        def _expire_rating_track_cache
          self.reload
          self.touch
        end

      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
loyal_core-0.0.11 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.10 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.8 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.7 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.6 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.5 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.4 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.3 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.2 lib/loyal_core/acts/rating_track_able.rb
loyal_core-0.0.1 lib/loyal_core/acts/rating_track_able.rb