rails_growth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +28 -0
  4. data/Rakefile +32 -0
  5. data/app/controllers/growth/admin/aim_entities_controller.rb +30 -0
  6. data/app/controllers/growth/admin/aim_logs_controller.rb +21 -0
  7. data/app/controllers/growth/admin/aim_users_controller.rb +30 -0
  8. data/app/controllers/growth/admin/aims_controller.rb +73 -0
  9. data/app/controllers/growth/admin/base_controller.rb +4 -0
  10. data/app/controllers/growth/admin/praise_incomes_controller.rb +61 -0
  11. data/app/controllers/growth/admin/reward_expenses_controller.rb +63 -0
  12. data/app/controllers/growth/admin/reward_incomes_controller.rb +59 -0
  13. data/app/controllers/growth/admin/rewards_controller.rb +65 -0
  14. data/app/controllers/growth/controller/application.rb +77 -0
  15. data/app/controllers/growth/my/aim_logs_controller.rb +45 -0
  16. data/app/controllers/growth/my/aim_users_controller.rb +25 -0
  17. data/app/controllers/growth/my/aims_controller.rb +44 -0
  18. data/app/controllers/growth/my/base_controller.rb +4 -0
  19. data/app/controllers/growth/my/gifts_controller.rb +27 -0
  20. data/app/controllers/growth/my/rewards_controller.rb +28 -0
  21. data/app/models/growth/aim.rb +5 -0
  22. data/app/models/growth/aim_code.rb +5 -0
  23. data/app/models/growth/aim_entity.rb +5 -0
  24. data/app/models/growth/aim_log.rb +5 -0
  25. data/app/models/growth/aim_user.rb +5 -0
  26. data/app/models/growth/model/aim.rb +52 -0
  27. data/app/models/growth/model/aim_code.rb +40 -0
  28. data/app/models/growth/model/aim_entity.rb +74 -0
  29. data/app/models/growth/model/aim_log.rb +79 -0
  30. data/app/models/growth/model/aim_user.rb +60 -0
  31. data/app/models/growth/model/card.rb +25 -0
  32. data/app/models/growth/model/cash.rb +17 -0
  33. data/app/models/growth/model/entity.rb +74 -0
  34. data/app/models/growth/model/praise_compute.rb +70 -0
  35. data/app/models/growth/model/praise_income.rb +82 -0
  36. data/app/models/growth/model/praise_user.rb +47 -0
  37. data/app/models/growth/model/promote.rb +30 -0
  38. data/app/models/growth/model/reward.rb +67 -0
  39. data/app/models/growth/model/reward_expense.rb +98 -0
  40. data/app/models/growth/model/reward_income.rb +35 -0
  41. data/app/models/growth/model/royalty_compute.rb +72 -0
  42. data/app/models/growth/model/user.rb +21 -0
  43. data/app/models/growth/praise_income.rb +7 -0
  44. data/app/models/growth/praise_user.rb +5 -0
  45. data/app/models/growth/reward.rb +5 -0
  46. data/app/models/growth/reward_expense.rb +5 -0
  47. data/app/models/growth/reward_income.rb +5 -0
  48. data/app/models/growth.rb +11 -0
  49. data/app/views/application/_growth_nav.html.erb +12 -0
  50. data/app/views/growth/admin/aim_entities/_filter.html.erb +10 -0
  51. data/app/views/growth/admin/aim_entities/index.html.erb +68 -0
  52. data/app/views/growth/admin/aim_entities/show.html.erb +22 -0
  53. data/app/views/growth/admin/aim_logs/_filter.html.erb +9 -0
  54. data/app/views/growth/admin/aim_logs/index.html.erb +62 -0
  55. data/app/views/growth/admin/aim_logs/show.html.erb +14 -0
  56. data/app/views/growth/admin/aim_users/_filter.html.erb +7 -0
  57. data/app/views/growth/admin/aim_users/index.html.erb +63 -0
  58. data/app/views/growth/admin/aim_users/show.html.erb +30 -0
  59. data/app/views/growth/admin/aims/_filter.html.erb +8 -0
  60. data/app/views/growth/admin/aims/_form.html.erb +14 -0
  61. data/app/views/growth/admin/aims/_item_field.html.erb +3 -0
  62. data/app/views/growth/admin/aims/_item_form.html.erb +24 -0
  63. data/app/views/growth/admin/aims/add_item.js.erb +9 -0
  64. data/app/views/growth/admin/aims/index.html.erb +54 -0
  65. data/app/views/growth/admin/aims/remove_item.js.erb +1 -0
  66. data/app/views/growth/admin/aims/show.html.erb +38 -0
  67. data/app/views/growth/admin/managers/_edit.html.erb +13 -0
  68. data/app/views/growth/admin/managers/_form.html.erb +34 -0
  69. data/app/views/growth/admin/managers/_new.html.erb +13 -0
  70. data/app/views/growth/admin/managers/_search_form.html.erb +8 -0
  71. data/app/views/growth/admin/managers/edit.js.erb +8 -0
  72. data/app/views/growth/admin/managers/index.html.erb +68 -0
  73. data/app/views/growth/admin/managers/new.js.erb +8 -0
  74. data/app/views/growth/admin/managers/show.html.erb +0 -0
  75. data/app/views/growth/admin/managers/update.js.erb +1 -0
  76. data/app/views/growth/admin/praise_incomes/_filter.html.erb +5 -0
  77. data/app/views/growth/admin/praise_incomes/_form.html.erb +3 -0
  78. data/app/views/growth/admin/praise_incomes/edit.html.erb +11 -0
  79. data/app/views/growth/admin/praise_incomes/index.html.erb +59 -0
  80. data/app/views/growth/admin/praise_incomes/new.html.erb +11 -0
  81. data/app/views/growth/admin/praise_incomes/show.html.erb +18 -0
  82. data/app/views/growth/admin/reward_expenses/_filter.html.erb +5 -0
  83. data/app/views/growth/admin/reward_expenses/_form.html.erb +7 -0
  84. data/app/views/growth/admin/reward_expenses/index.html.erb +49 -0
  85. data/app/views/growth/admin/reward_expenses/show.html.erb +26 -0
  86. data/app/views/growth/admin/reward_incomes/_filter.html.erb +10 -0
  87. data/app/views/growth/admin/reward_incomes/_form.html.erb +3 -0
  88. data/app/views/growth/admin/reward_incomes/edit.html.erb +11 -0
  89. data/app/views/growth/admin/reward_incomes/index.html.erb +49 -0
  90. data/app/views/growth/admin/reward_incomes/new.html.erb +11 -0
  91. data/app/views/growth/admin/reward_incomes/show.html.erb +18 -0
  92. data/app/views/growth/admin/rewards/_filter.html.erb +14 -0
  93. data/app/views/growth/admin/rewards/_form.html.erb +27 -0
  94. data/app/views/growth/admin/rewards/index.html.erb +84 -0
  95. data/app/views/growth/admin/rewards/show.html.erb +26 -0
  96. data/app/views/growth/my/aims/_aim.json.jbuilder +6 -0
  97. data/app/views/growth/my/aims/index.json.jbuilder +3 -0
  98. data/app/views/growth/my/aims/show.json.jbuilder +1 -0
  99. data/app/views/growth/my/gifts/_gift.json.jbuilder +1 -0
  100. data/app/views/growth/my/gifts/_praise_income.json.jbuilder +8 -0
  101. data/app/views/growth/my/gifts/give.json.jbuilder +8 -0
  102. data/app/views/growth/my/gifts/index.json.jbuilder +9 -0
  103. data/app/views/growth/my/rewards/_praise_user.json.jbuilder +2 -0
  104. data/app/views/growth/my/rewards/top.json.jbuilder +6 -0
  105. data/config/locales/en.notify.yml +7 -0
  106. data/config/locales/en.yml +29 -0
  107. data/config/locales/zh.controller.yml +24 -0
  108. data/config/locales/zh.notify.yml +7 -0
  109. data/config/locales/zh.yml +11 -0
  110. data/config/routes.rb +57 -0
  111. data/lib/rails_growth/config.rb +13 -0
  112. data/lib/rails_growth/engine.rb +19 -0
  113. data/lib/rails_growth/serial_number_helper.rb +28 -0
  114. data/lib/rails_growth.rb +4 -0
  115. metadata +170 -0
@@ -0,0 +1,65 @@
1
+ module Growth
2
+ class Admin::RewardsController < Admin::BaseController
3
+ before_action :set_reward, only: [:show, :edit, :update, :destroy]
4
+
5
+ def index
6
+ q_params = {}
7
+ q_params.merge! params.permit(:entity_type, :entity_id)
8
+
9
+ @rewards = Reward.default_where(q_params).order(id: :desc).page(params[:page])
10
+ end
11
+
12
+ def new
13
+ @reward = Reward.new
14
+ end
15
+
16
+ def create
17
+ @reward = Reward.new(reward_params)
18
+
19
+ if @reward.save
20
+ render 'create'
21
+ else
22
+ render :new, locals: { model: @reward }, status: :unprocessable_entity
23
+ end
24
+ end
25
+
26
+ def show
27
+ end
28
+
29
+ def edit
30
+ end
31
+
32
+ def update
33
+ @reward.assign_attributes(reward_params)
34
+
35
+ if @reward.save
36
+ render 'update'
37
+ else
38
+ render :edit, locals: { model: @reward }, status: :unprocessable_entity
39
+ end
40
+ end
41
+
42
+ def destroy
43
+ @reward.destroy
44
+ end
45
+
46
+ private
47
+ def set_reward
48
+ @reward = Reward.find(params[:id])
49
+ end
50
+
51
+ def reward_params
52
+ params.fetch(:reward, {}).permit(
53
+ :amount,
54
+ :entity_type,
55
+ :entity_id,
56
+ :max_piece,
57
+ :min_piece,
58
+ :start_at,
59
+ :finish_at,
60
+ :enabled
61
+ )
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,77 @@
1
+ module Growth
2
+ module Controller::Application
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ after_action :growth_record
7
+ end
8
+
9
+ def aim_user
10
+ current_user
11
+ end
12
+
13
+ def growth_api(code, entity_type, entity_id)
14
+ aim_ids = AimCode.growth_hash[code]
15
+ return if aim_ids.blank?
16
+
17
+ Aim.where(id: aim_ids).map do |aim|
18
+ if aim_user
19
+ sn = RailsGrowth::SerialNumberHelper.result(Time.now, aim.repeat_type)
20
+ au = aim_user.aim_users.find_by(aim_id: aim.id, serial_number: sn)
21
+ ae = aim_user.aim_entities.find_by(aim_id: aim.id, serial_number: sn, entity_type: entity_type, entity_id: entity_id)
22
+ next if au&.task_done? && ae
23
+ aim_log = aim_user.aim_logs.build(aim_id: aim.id)
24
+ else
25
+ next unless aim.verbose
26
+ aim_log = AimLog.new(aim_id: aim.id)
27
+ end
28
+
29
+ aim_log.ip = request.remote_ip
30
+ aim_log.entity_type = entity_type
31
+ aim_log.entity_id = entity_id
32
+ aim_log.code = code
33
+ aim_log.save!
34
+ aim_log
35
+ end.compact
36
+ end
37
+
38
+ def growth_log(code)
39
+ growth_api(code, params[:entity_type], params[:entity_id])
40
+ end
41
+
42
+ def growth_record
43
+ code = [controller_path, action_name].join('#')
44
+ entity_type = growth_entity_type
45
+ entity_id = growth_entity_id
46
+ r = growth_api(code, entity_type, entity_id)
47
+ growth_response(r) if r.present?
48
+ end
49
+
50
+ def growth_response(r)
51
+ reward_amount = r.select(&:rewarded).sum { |i| i.aim_entity.reward_amount }
52
+ rewardable_codes = []
53
+ unless r.blank?
54
+ rewardable_codes = r[0].entity.rewardable_codes(aim_user.id)
55
+ end
56
+
57
+ if reward_amount > 0
58
+ reward = {
59
+ amount: reward_amount,
60
+ code: 'success',
61
+ rewardable_codes: rewardable_codes
62
+ }
63
+ body = JSON.parse(self.response_body[0])
64
+ self.response_body = body.merge(reward: reward).to_json
65
+ end
66
+ end
67
+
68
+ def growth_entity_type
69
+ controller_name.classify
70
+ end
71
+
72
+ def growth_entity_id
73
+ params.fetch('id', nil)
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,45 @@
1
+ module Growth
2
+ class My::AimLogsController < My::BaseController
3
+ before_action :set_aim_log, only: [:show]
4
+
5
+ def show
6
+ render json: @aim_log
7
+ end
8
+
9
+ def create
10
+ r = growth_log(params[:code])
11
+ rewardable_codes = []
12
+ unless r.blank?
13
+ rewardable_codes = r[0].entity.rewardable_codes(aim_user.id)
14
+ end
15
+ if r.present?
16
+ @reward_amount = r.select(&:rewarded).sum { |i| i.aim_entity.reward_amount }
17
+ end
18
+
19
+ if @reward_amount.to_d > 0
20
+ reward = {
21
+ amount: @reward_amount,
22
+ code: 'success',
23
+ rewardable_codes: rewardable_codes
24
+ }
25
+ render json: { aim_logs: r, reward: reward }, status: :created
26
+ elsif r.present?
27
+ render json: { aim_logs: r }, status: :created
28
+ else
29
+ # todo
30
+ render json: { reward: { amount: 0, code: 'over_limit' } }, status: :ok
31
+ end
32
+ end
33
+
34
+ private
35
+ def set_aim_log
36
+ @aim_log = AimLog.find(params[:id])
37
+ end
38
+
39
+ def aim_log_params
40
+ params.permit(
41
+ :code
42
+ )
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ module Growth
2
+ class My::AimUsersController < My::BaseController
3
+ before_action :set_aim_user, only: [:show]
4
+
5
+ def index
6
+ @aim_users = AimUser.page(params[:page]).per(params[:per])
7
+
8
+ render json: { aim_users: @aim_users }
9
+ end
10
+
11
+ def show
12
+ render json: @aim_user
13
+ end
14
+
15
+ private
16
+ def set_aim_user
17
+ @aim_user = AimUser.find(params[:id])
18
+ end
19
+
20
+ def aim_user_params
21
+ params.fetch(:aim_user, {})
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ module Growth
2
+ class My::AimsController < My::BaseController
3
+ before_action :set_aim, only: [:show, :update, :destroy]
4
+
5
+ def index
6
+ @aims = Aim.page(params[:page]).per(params[:per])
7
+ end
8
+
9
+ def create
10
+ @aim = Aim.new(aim_params)
11
+
12
+ if @aim.save
13
+ render json: @aim, status: :created, location: @aim
14
+ else
15
+ render json: @aim.errors, status: :unprocessable_entity
16
+ end
17
+ end
18
+
19
+ def show
20
+ render json: @aim.as_json(root: true)
21
+ end
22
+
23
+ def update
24
+ if @aim.update(aim_params)
25
+ render json: @aim
26
+ else
27
+ render json: @aim.errors, status: :unprocessable_entity
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ @aim.destroy
33
+ end
34
+
35
+ private
36
+ def set_aim
37
+ @aim = Aim.find(params[:id])
38
+ end
39
+
40
+ def aim_params
41
+ params.fetch(:aim, {})
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ module Growth
2
+ class My::BaseController < MyController
3
+ end
4
+ end
@@ -0,0 +1,27 @@
1
+ module Growth
2
+ class My::GiftsController < My::BaseController
3
+ before_action :set_gift, only: [:give]
4
+ before_action :require_login, only: [:give]
5
+
6
+ def index
7
+ @gifts = Gift.order(praise_incomes_count: :desc).page(params[:page]).per(params[:per])
8
+ end
9
+
10
+ def give
11
+ @reward = Reward.find_or_create_by!(entity_type: params[:entity_type], entity_id: params[:entity_id])
12
+ @praise_income = @gift.give_to(@reward, current_user)
13
+
14
+ if @praise_income.persisted?
15
+ render 'give'
16
+ else
17
+ process_errors(@praise_income)
18
+ end
19
+ end
20
+
21
+ private
22
+ def set_gift
23
+ @gift = Gift.find(params[:id])
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module Growth
2
+ class My::RewardsController < My::BaseController
3
+ before_action :set_reward, only: [:top]
4
+
5
+ def top
6
+ @praise_users = @reward.praise_users.includes(:user).order(amount: :desc).page(params[:page])
7
+
8
+ if current_user
9
+ @praise_user = @praise_users.find_by(user_id: current_user.id)
10
+ end
11
+ end
12
+
13
+ def broadcast
14
+ @praise_amounts = PraiseIncome.top
15
+ end
16
+
17
+ private
18
+ def set_reward
19
+ if params[:id]
20
+ @reward = Reward.find params[:id]
21
+ elsif params[:entity_type] && params[:entity_id]
22
+ @reward = Reward.find_or_initialize_by(entity_type: params[:entity_type], entity_id: params[:entity_id])
23
+ @reward.save_with_amount
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module Growth
2
+ class Aim < ApplicationRecord
3
+ include Model::Aim
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Growth
2
+ class AimCode < ApplicationRecord
3
+ include Model::AimCode
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Growth
2
+ class AimEntity < ApplicationRecord
3
+ include Model::AimEntity
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Growth
2
+ class AimLog < ApplicationRecord
3
+ include Model::AimLog
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Growth
2
+ class AimUser < ApplicationRecord
3
+ include Model::AimUser
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ module Growth
2
+ module Model::Aim
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attribute :name, :string
7
+ attribute :unit, :string
8
+ attribute :repeat_type, :string
9
+ attribute :rate, :decimal, precision: 10, scale: 2, default: 1
10
+ attribute :task_point, :integer, default: 0
11
+ attribute :reward_point, :integer, default: 0
12
+ attribute :reward_amount, :integer, default: 0
13
+ attribute :verbose, :boolean
14
+
15
+ has_many :aim_codes, dependent: :delete_all
16
+ has_many :aim_users, dependent: :nullify
17
+ has_many :aim_entities, dependent: :nullify
18
+ has_many :aim_logs, dependent: :nullify
19
+
20
+ accepts_nested_attributes_for :aim_codes, allow_destroy: true, reject_if: :all_blank
21
+
22
+ enum repeat_type: {
23
+ once: 'once',
24
+ daily: 'daily',
25
+ weekly: 'weekly',
26
+ monthly: 'monthly',
27
+ yearly: 'yearly',
28
+ forever: 'forever'
29
+ }
30
+
31
+ scope :reward, -> { default_where('reward_point-gt': 0) }
32
+ scope :task, -> { default_where('task_point-gt': 0) }
33
+ end
34
+
35
+ def aim_user(user_id)
36
+ self.aim_users.find { |i| i.user_id == user_id }
37
+ end
38
+
39
+ def serial_number(timestamp)
40
+ RailsGrowth::SerialNumberHelper.result(timestamp, repeat_type)
41
+ end
42
+
43
+ class_methods do
44
+
45
+ def reward_codes
46
+ includes(:aim_codes).reward.map { |i| i.aim_codes.pluck(:code) }.flatten
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,40 @@
1
+ module Growth
2
+ module Model::AimCode
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attribute :controller_path, :string
7
+ attribute :action_name, :string
8
+ attribute :code, :string, null: false
9
+
10
+ belongs_to :aim
11
+
12
+ validates :code, uniqueness: { scope: :aim_id }, presence: true
13
+ before_validation :sync_code
14
+ after_commit :delete_cache
15
+ end
16
+
17
+ def sync_code
18
+ if self.controller_path.present? && self.action_name.present?
19
+ self.code = [self.controller_path, self.action_name].join('#')
20
+ end
21
+ end
22
+
23
+ def delete_cache
24
+ if Rails.cache.delete('rails_growth')
25
+ logger.debug "-----> Cache key rails_growth deleted"
26
+ end
27
+ end
28
+
29
+ class_methods do
30
+
31
+ def growth_hash
32
+ Rails.cache.fetch('rails_growth', {}) do
33
+ AimCode.pluck(:code, :aim_id).to_array_h.to_combine_h
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end