grade 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43e5f548b8180e7311dae6edd9964c046147c5ac7fbc9715ef540d1c9a6ff180
4
+ data.tar.gz: 84cd337c74fb8acacb3f57a9aec8c57a34d2aac0e6e152668d8de2d1e00abf76
5
+ SHA512:
6
+ metadata.gz: a6abbb2cc4cee6a473a5370c89c806a1da2e2a5bce5d218abc4d975b1980b050417243f95ede9c790fbe9edfc055d881958dbdfd9855a0ebb828d8895b6719c7
7
+ data.tar.gz: '069834659f5756597a2fdf978e10e6550c3c33872efaf3aa34a2324ccd3c06d194b98c8c65891c1128d72bc400f6ad2025d7cb740291675ab8d16f062045a3ac'
@@ -0,0 +1,20 @@
1
+ Copyright 2020 aaronliu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ # dreamland-grade
2
+ Dreamland grade&score engine
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Grade'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/grade .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ module Grade
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Grade
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Grade
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Grade
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Grade
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module Grade
2
+ class Rule < ApplicationRecord
3
+ validates :action, presence: true, uniqueness: true
4
+ validates :change_type, presence: true
5
+
6
+ enum change_type: { increase: 0, decrease: 1 }
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ module Grade
2
+ class UserScore < ApplicationRecord
3
+ def increase_score(change_score, message)
4
+ new_score = self.score + change_score
5
+ self.update(score: new_score)
6
+ options = {
7
+ message: message,
8
+ user_id: self.user_id,
9
+ score: change_score,
10
+ after_score: new_score,
11
+ change_type: :increase
12
+ }
13
+ generate_log(options)
14
+ end
15
+
16
+ def decrease_score(change_score, message)
17
+ new_score = self.score - change_score
18
+ self.update(score: new_score)
19
+ options = {
20
+ message: message,
21
+ user_id: self.user_id,
22
+ score: change_score,
23
+ after_score: new_score,
24
+ change_type: :decrease
25
+ }
26
+ generate_log(options)
27
+ end
28
+
29
+ private
30
+
31
+ def generate_log(options)
32
+ UserScoreLog.create(options)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Grade
2
+ class UserScoreLog < ApplicationRecord
3
+ enum change_type: { increase: 0, decrease: 1 }
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ module Grade
2
+ class ScoreService
3
+ def self.current_score(user_id)
4
+ return 0 if user_id.nil?
5
+ user_score = Grade::UserScore.find_or_create_by(user_id: user_id)
6
+ user_score.score
7
+ end
8
+
9
+ def self.score_logs(user_id)
10
+ Grade::UserScoreLog.where(user_id: user_id)
11
+ end
12
+
13
+ def self.change_score(user_id, action)
14
+ return if user_id.nil?
15
+ user_score = Grade::UserScore.find_or_create_by(user_id: user_id)
16
+ rule = Grade::Rule.find_by(action: action)
17
+ return if rule.nil?
18
+ if rule.increase?
19
+ user_score.increase_score(rule.score, rule.message)
20
+ else
21
+ user_score.decrease_score(rule.score, rule.message)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Grade</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "grade/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,2 @@
1
+ Grade::Engine.routes.draw do
2
+ end
@@ -0,0 +1,10 @@
1
+ class CreateGradeUserScores < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :grade_user_scores do |t|
4
+ t.integer :score, default: 0, comment: "用户积分"
5
+ t.integer :user_id, comment: "user_id"
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ class CreateGradeUserScoreLogs < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :grade_user_score_logs do |t|
4
+ t.integer :user_id, comment: "user_id"
5
+ t.string :message, comment: "积分变化消息"
6
+ t.integer :score, comment: "变动积分"
7
+ t.integer :after_score, comment: "变化后积分"
8
+ t.integer :change_type, comment: "增加或者减少"
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ class CreateGradeRules < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :grade_rules do |t|
4
+ t.string :action, uniq: true
5
+ t.string :message
6
+ t.integer :score
7
+ t.integer :change_type
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ require "grade/engine"
2
+
3
+ module Grade
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,5 @@
1
+ module Grade
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Grade
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Grade
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,14 @@
1
+ task :grade_init_rule do
2
+ Grade::Rule.create(action: "register", message: "注册", score: 5, change_type: :increase)
3
+ Grade::Rule.create(action: "followed", message: "被关注", score: 3, change_type: :increase)
4
+ Grade::Rule.create(action: "unfollowed", message: "被取消关注", score: 3, change_type: :decrease)
5
+ Grade::Rule.create(action: "signin", message: "签到", score: 2, change_type: :increase)
6
+ Grade::Rule.create(action: "create_topic", message: "发帖", score: 10, change_type: :increase)
7
+ Grade::Rule.create(action: "delete_topic", message: "删帖", score: 10, change_type: :decrease)
8
+ Grade::Rule.create(action: "add_precision", message: "加精", score: 4, change_type: :increase)
9
+ Grade::Rule.create(action: "remove_precision", message: "取消加精", score: 4, change_type: :decrease)
10
+ Grade::Rule.create(action: "topic_break_rule", message: "帖子违规", score: 20, change_type: :decrease)
11
+ Grade::Rule.create(action: "creat_comment", message: "发布评论", score: 2, change_type: :increase)
12
+ Grade::Rule.create(action: "delete_comment", message: "删除评论", score: 2, change_type: :decrease)
13
+ Grade::Rule.create(action: "comment_break_rule", message: "评论违规", score: 10, change_type: :decrease)
14
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grade
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aaronliu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - 935030733@qq.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/assets/config/grade_manifest.js
52
+ - app/assets/stylesheets/grade/application.css
53
+ - app/controllers/grade/application_controller.rb
54
+ - app/helpers/grade/application_helper.rb
55
+ - app/jobs/grade/application_job.rb
56
+ - app/mailers/grade/application_mailer.rb
57
+ - app/models/grade/application_record.rb
58
+ - app/models/grade/rule.rb
59
+ - app/models/grade/user_score.rb
60
+ - app/models/grade/user_score_log.rb
61
+ - app/services/grade/score_service.rb
62
+ - app/views/layouts/grade/application.html.erb
63
+ - config/routes.rb
64
+ - db/migrate/20201201131134_create_grade_user_scores.rb
65
+ - db/migrate/20201201131239_create_grade_user_score_logs.rb
66
+ - db/migrate/20201201233313_create_grade_rules.rb
67
+ - lib/grade.rb
68
+ - lib/grade/engine.rb
69
+ - lib/grade/version.rb
70
+ - lib/tasks/grade_tasks.rake
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org/
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: ''
95
+ test_files: []