Skip to content

Commit

Permalink
Provide rake task to integrate multiple method_call_situations records
Browse files Browse the repository at this point in the history
  • Loading branch information
shakemurasan committed Feb 6, 2017
1 parent 79ccce7 commit c329d31
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# CHANGELOG
## [0.2.3](https://github.com/muramurasan/okuribito_rails/releases/tag/v0.2.3) (xxxxxxx xx, 2017)
## [0.2.4](https://github.com/muramurasan/okuribito_rails/releases/tag/v0.2.4) (xxxxxxx xx, 2017)
* Fix the following issue:
** Provide rake task to integrate multiple method_call_situations records #51
** method_call_situation don't allow duplicated registration #52

## [0.2.3](https://github.com/muramurasan/okuribito_rails/releases/tag/v0.2.3) (January 26, 2017)
Expand Down
1 change: 1 addition & 0 deletions app/models/okuribito_rails/method_call_situation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MethodCallSituation < ActiveRecord::Base
->(num) { where("created_at <= ?", Time.zone.today.days_ago(num).end_of_day) }
scope :with_uncalled_method, -> { where(called_num: 0) }
scope :with_called_method, -> { where(called_num: 1..Float::INFINITY) }
scope :group_by_method, -> { group("class_name", "method_symbol", "method_name") }

def self.search(args)
mcs = self
Expand Down
25 changes: 21 additions & 4 deletions lib/tasks/okuribito_rails_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# desc "Explaining what the task does"
# task :okuribito_rails do
# # Task goes here
# end
namespace :okuribito_rails do
desc "Integrate multiple method_call_situations records"
task integrate_method_call_situations: :environment do
puts "Integrate multiple method_call_situations records...."

OkuribitoRails::MethodCallSituation.group_by_method.each do |situation|
puts "#{situation.class_name}#{situation.method_symbol}#{situation.method_name}"
situations = OkuribitoRails::MethodCallSituation.where(class_name: situation.class_name,
method_symbol: situation.method_symbol,
method_name: situation.method_name)
if situations.size > 1
sum = situations.pluck(:called_num).inject(0) { |a, e| a + e }
situations[0].update(called_num: sum)
situations[1..-1].each(&:destroy)
puts " integrate #{situations.size} records..."
else
puts " nothing to do..."
end
end
end
end

0 comments on commit c329d31

Please sign in to comment.