acts_as_footprintable 0.4.0 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/gem-push.yml +30 -0
  3. data/.github/workflows/test.yml +42 -0
  4. data/.gitignore +18 -20
  5. data/.rubocop.yml +32 -0
  6. data/Gemfile +5 -15
  7. data/README.md +6 -8
  8. data/Rakefile +11 -7
  9. data/acts_as_footprintable.gemspec +23 -19
  10. data/config.ru +9 -0
  11. data/gemfiles/rails_5.0.gemfile +8 -0
  12. data/gemfiles/rails_5.1.gemfile +8 -0
  13. data/gemfiles/rails_5.2.gemfile +8 -0
  14. data/gemfiles/rails_6.0.gemfile +8 -0
  15. data/gemfiles/rails_6.1.gemfile +8 -0
  16. data/gemfiles/rails_7.0.gemfile +8 -0
  17. data/lib/acts_as_footprintable/extenders/footprintable.rb +5 -4
  18. data/lib/acts_as_footprintable/extenders/footprinter.rb +4 -3
  19. data/lib/acts_as_footprintable/footprint.rb +9 -14
  20. data/lib/acts_as_footprintable/footprintable.rb +6 -10
  21. data/lib/acts_as_footprintable/footprinter.rb +32 -19
  22. data/lib/acts_as_footprintable/version.rb +3 -2
  23. data/lib/acts_as_footprintable.rb +2 -1
  24. data/lib/generators/acts_as_footprintable/migration/migration_generator.rb +20 -9
  25. data/lib/generators/acts_as_footprintable/migration/templates/active_record/migration.rb +6 -5
  26. data/test/footprintable_test.rb +97 -0
  27. data/test/footprinter_test.rb +64 -0
  28. data/test/internal/app/models/footprintable.rb +6 -0
  29. data/test/internal/app/models/not_footprintable.rb +5 -0
  30. data/{spec → test}/internal/app/models/not_user.rb +3 -2
  31. data/{spec → test}/internal/app/models/second_footprintable.rb +4 -3
  32. data/{spec → test}/internal/app/models/user.rb +3 -2
  33. data/test/test_helper.rb +57 -0
  34. metadata +82 -60
  35. data/.rspec +0 -1
  36. data/.travis.yml +0 -16
  37. data/spec/footprintable_spec.rb +0 -98
  38. data/spec/footprinter_spec.rb +0 -63
  39. data/spec/internal/app/models/footprintable.rb +0 -5
  40. data/spec/internal/app/models/not_footprintable.rb +0 -4
  41. data/spec/internal/config/database.yml +0 -5
  42. data/spec/internal/db/schema.rb +0 -31
  43. data/spec/internal/log/.gitignore +0 -1
  44. data/spec/spec_helper.rb +0 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 23d90af2f93fa15288f683b8376f4c4761918d5e
4
- data.tar.gz: 5aab8f4d0f88e7e94031ff766b675d809a5db676
2
+ SHA256:
3
+ metadata.gz: a4dda61f4d4e76bc6ee5251d8591b51f6888e7d70609cb16d21601faa07d2014
4
+ data.tar.gz: '0148bca9da729db5fe61d7c92da7c84ae5e0812ea15d49e73fdb328d37d4ba1e'
5
5
  SHA512:
6
- metadata.gz: 0e4017dc629bd48cce469b8ed3d1d104e2300e76a53f9e64e4b1a74bde6cf1c4c2e99756ae52c5dfe6a692b4cb08c1f93d0635ff9a2b9ede5250deb731193720
7
- data.tar.gz: '08b29b1c8e2b4424016c544eb27e41878f3b773f36316d9fefb7756cd6027f0ea5c3d8c7c750c805001f0e7aa52aeb3e2bdda6bce8db4f58c41ba1e78394e0db'
6
+ metadata.gz: a96376414507b2bb3067460c0f0a359afe11afe92ebb863dd2d69d8b7dd9cc82ada52c5e2a53f3f21b871780bc4edf71d1b57257c7af7bcc1232da8e99c13bde
7
+ data.tar.gz: a239b306ad678c186d65284c50099d2183eded818c78723ffdaa04f05819a110d7cba5f8e838d0aa100d9efe5d5c87563db753b95f400067b6572b4fb6e9d9b1
@@ -0,0 +1,30 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ workflow_dispatch:
5
+
6
+ jobs:
7
+ build:
8
+ name: Build + Publish
9
+ runs-on: ubuntu-latest
10
+ permissions:
11
+ contents: read
12
+ packages: write
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.6.x
20
+
21
+ - name: Publish to RubyGems
22
+ run: |
23
+ mkdir -p $HOME/.gem
24
+ touch $HOME/.gem/credentials
25
+ chmod 0600 $HOME/.gem/credentials
26
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
27
+ gem build *.gemspec
28
+ gem push *.gem
29
+ env:
30
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
@@ -0,0 +1,42 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby: ['2.6', '2.7', '3.0', '3.1']
15
+ gemfile: ['rails_5.0', 'rails_5.1', 'rails_5.2', 'rails_6.0', 'rails_6.1', 'rails_7.0']
16
+ exclude:
17
+ - ruby: '2.6'
18
+ gemfile: 'rails_7.0'
19
+ - ruby: '3.0'
20
+ gemfile: 'rails_5.0'
21
+ - ruby: '3.0'
22
+ gemfile: 'rails_5.1'
23
+ - ruby: '3.0'
24
+ gemfile: 'rails_5.2'
25
+ - ruby: '3.1'
26
+ gemfile: 'rails_5.0'
27
+ - ruby: '3.1'
28
+ gemfile: 'rails_5.1'
29
+ - ruby: '3.1'
30
+ gemfile: 'rails_5.2'
31
+ env:
32
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
33
+
34
+ steps:
35
+ - uses: actions/checkout@v2
36
+ - name: Set up Ruby
37
+ uses: ruby/setup-ruby@v1
38
+ with:
39
+ ruby-version: ${{ matrix.ruby }}
40
+ bundler-cache: true
41
+ - name: Run tests
42
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -1,21 +1,19 @@
1
- *.iml
2
- .DS_Store
3
- .idea
4
- *.gem
5
- *.rbc
6
- .bundle
7
- .config
8
- .yardoc
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /log/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .ruby-version
9
14
  Gemfile.lock
10
- InstalledFiles
11
- _yardoc
12
- coverage
13
- doc/
14
- lib/bundler/man
15
- pkg
16
- rdoc
17
- spec/reports
18
- test/tmp
19
- test/version_tmp
20
- tmp
21
- *.log
15
+ .circleci/config.processed.yml
16
+
17
+ /gemfiles/*.gemfile.lock
18
+ /gemfiles/.bundle
19
+ *.sqlite3
data/.rubocop.yml ADDED
@@ -0,0 +1,32 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-minitest
5
+
6
+ inherit_from: .rubocop_todo.yml
7
+
8
+ AllCops:
9
+ NewCops: enable
10
+ TargetRubyVersion: 2.6
11
+ Exclude:
12
+ - 'lib/generators/acts_as_footprintable/migration/templates/active_record/migration.rb'
13
+
14
+ Layout/LineLength:
15
+ AllowHeredoc: true
16
+ AllowURI: true
17
+
18
+ Metrics/MethodLength:
19
+ IgnoredMethods:
20
+ - acts_as_footprintable
21
+ - recent_footprint_ids
22
+
23
+ Metrics/BlockLength:
24
+ IgnoredMethods:
25
+ - describe
26
+ - refine
27
+
28
+ Layout/DotPosition:
29
+ EnforcedStyle: trailing
30
+
31
+ Style/Documentation:
32
+ Enabled: false
data/Gemfile CHANGED
@@ -1,20 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  source 'https://rubygems.org'
4
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
3
5
 
4
6
  # Specify your gem's dependencies in acts_as_footprintable.gemspec
5
7
  gemspec
6
8
 
7
- rails_version = ENV['RAILS_VERSION'] || 'default'
8
- rails = case rails_version
9
- when 'master'
10
- { :github => 'rails/rails' }
11
- when 'default'
12
- '~> 3.2.0'
13
- else
14
- "~> #{rails_version}"
15
- end
16
-
17
- gem 'rails', rails
18
- group :development do
19
- gem 'combustion', '~> 0.5'
20
- end
9
+ gem 'activerecord'
10
+ gem 'sqlite3'
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # ActsAsFootprintable
2
- [![Build Status](https://travis-ci.org/patorash/acts_as_footprintable.png)](https://travis-ci.org/patorash/acts_as_footprintable)
2
+
3
+ [![Test](https://github.com/patorash/acts_as_footprintable/actions/workflows/test.yml/badge.svg)](https://github.com/patorash/acts_as_footprintable/actions/workflows/test.yml)
3
4
 
4
5
  Acts As Footprintable is a Ruby Gem specifically written for Rails/ActiveRecord models.
5
6
  The main goals of this gem are:
@@ -11,12 +12,12 @@ The main goals of this gem are:
11
12
 
12
13
  ## Installation
13
14
 
14
- ### Rails 3.2 and 4.0+
15
+ ### Rails 5.x, 6.x and 7.0
15
16
 
16
17
  Add this line to your application's Gemfile:
17
18
 
18
19
  ```ruby
19
- gem 'acts_as_footprintable', '~> 0.4.0'
20
+ gem 'acts_as_footprintable', '~> 0.6.0'
20
21
  ```
21
22
 
22
23
  And then execute:
@@ -44,7 +45,7 @@ class Post < ActiveRecord::Base
44
45
  acts_as_footprintable
45
46
  end
46
47
 
47
- @post = Post.create(:name => 'my post!')
48
+ @post = Post.create(name: 'my post!')
48
49
 
49
50
  @post.leave_footprints @user
50
51
  @post.footprints.size # => 1
@@ -91,10 +92,7 @@ post_access_histories = @user.access_histories_for(Post, 10)
91
92
 
92
93
  ## Testing
93
94
 
94
- All tests follow the RSpec format and located in the spec directory.
95
- They can be run with:
96
-
97
- $ rake spec
95
+ $ bundle exec rake
98
96
 
99
97
  ## Contributing
100
98
 
data/Rakefile CHANGED
@@ -1,10 +1,14 @@
1
- # coding: utf-8
2
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
3
2
 
4
- require 'rspec/core/rake_task'
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
5
 
6
- desc "Run specs"
7
- RSpec::Core::RakeTask.new(:spec)
6
+ task default: [:test]
8
7
 
9
- desc 'Default: run specs.'
10
- task :default => :spec
8
+ Rake::TestTask.new do |test|
9
+ test.libs << 'lib'
10
+ test.libs << 'test'
11
+ test.test_files = Dir['test/**/*_test.rb']
12
+ test.warning = false
13
+ # test.verbose = true
14
+ end
@@ -1,29 +1,33 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
4
+ lib = File.expand_path('lib', __dir__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require 'acts_as_footprintable/version'
5
7
 
6
8
  Gem::Specification.new do |spec|
7
- spec.name = "acts_as_footprintable"
9
+ spec.name = 'acts_as_footprintable'
8
10
  spec.version = ActsAsFootprintable::VERSION
9
- spec.author = "Toyoaki Oko"
10
- spec.email = "chariderpato@gmail.com"
11
- spec.description = %q{Rails gem to allowing records to leave footprints}
12
- spec.summary = %q{Rails gem to allowing records to leave footprints}
13
- spec.homepage = "https://github.com/patorash/acts_as_footprintable"
14
- spec.license = "MIT"
11
+ spec.author = 'Toyoaki Oko'
12
+ spec.email = 'chariderpato@gmail.com'
13
+ spec.description = 'Rails gem to allowing records to leave footprints'
14
+ spec.summary = 'Rails gem to allowing records to leave footprints'
15
+ spec.homepage = 'https://github.com/patorash/acts_as_footprintable'
16
+ spec.license = 'MIT'
17
+ spec.metadata['rubygems_mfa_required'] = 'true'
15
18
 
16
- spec.files = `git ls-files`.split($/)
19
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
20
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
22
+ spec.require_paths = ['lib']
23
+ spec.required_ruby_version = '>= 2.6'
20
24
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'rspec', ">= 3.0.0"
24
- spec.add_development_dependency 'pg'
25
- spec.add_development_dependency 'timecop'
26
- spec.add_development_dependency 'database_cleaner', "~> 1.0.1"
27
- spec.add_development_dependency 'pry'
28
- #spec.add_development_dependency 'combustion', '~> 0.5.1'
25
+ spec.add_dependency 'activerecord', '< 7.1.0', '>= 5.0.0'
26
+ spec.add_development_dependency 'database_cleaner', '~> 2.0'
27
+ spec.add_development_dependency 'rake', '~> 13.0'
28
+ spec.add_development_dependency 'rubocop', '~> 1.24'
29
+ spec.add_development_dependency 'rubocop-minitest', '~> 0.17'
30
+ spec.add_development_dependency 'rubocop-performance', '~> 1.13'
31
+ spec.add_development_dependency 'rubocop-rake', '~> 0.6'
32
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
29
33
  end
data/config.ru ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require :default, :development
7
+
8
+ Combustion.initialize! :all
9
+ run Combustion::Application
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 5.0.0'
6
+ gem 'sqlite3', '~> 1.3.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 5.1.0'
6
+ gem 'sqlite3', '~> 1.3.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 5.2.0'
6
+ gem 'sqlite3', '~> 1.4'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 6.0.0'
6
+ gem 'sqlite3', '~> 1.4'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 6.1.0'
6
+ gem 'sqlite3', '~> 1.4'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'activerecord', '~> 7.0.1'
6
+ gem 'sqlite3', '~> 1.4'
7
+
8
+ gemspec path: '../'
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActsAsFootprintable
3
4
  module Extenders
4
5
  module Footprintable
@@ -15,9 +16,9 @@ module ActsAsFootprintable
15
16
  true
16
17
  end
17
18
 
18
- def self.access_ranking(range=nil, limit=nil)
19
+ def self.access_ranking(range = nil, limit = nil)
19
20
  records = Footprint.for_type(self)
20
- records = records.where(:created_at => range) unless range.nil?
21
+ records = records.where(created_at: range) unless range.nil?
21
22
  records = records.limit(limit) unless limit.nil?
22
23
  records.group(:footprintable_id).order('count_footprintable_id desc').count(:footprintable_id)
23
24
  end
@@ -25,4 +26,4 @@ module ActsAsFootprintable
25
26
  end
26
27
  end
27
28
  end
28
- end
29
+ end
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActsAsFootprintable
3
4
  module Extenders
4
5
  module Footprinter
@@ -6,7 +7,7 @@ module ActsAsFootprintable
6
7
  false
7
8
  end
8
9
 
9
- def acts_as_footprinter(*args)
10
+ def acts_as_footprinter(*_args)
10
11
  require 'acts_as_footprintable/footprinter'
11
12
  include ActsAsFootprintable::Footprinter
12
13
 
@@ -18,4 +19,4 @@ module ActsAsFootprintable
18
19
  end
19
20
  end
20
21
  end
21
- end
22
+ end
@@ -1,19 +1,14 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActsAsFootprintable
3
4
  class Footprint < ::ActiveRecord::Base
4
- if ::ActiveRecord::VERSION::MAJOR < 4
5
- attr_accessible :footprintable_id, :footprintable_type,
6
- :footprinter_id, :footprinter_type,
7
- :footprintable, :footprinter
8
- end
9
-
10
- belongs_to :footprintable, :polymorphic => true
11
- belongs_to :footprinter, :polymorphic => true
5
+ belongs_to :footprintable, polymorphic: true
6
+ belongs_to :footprinter, polymorphic: true
12
7
 
13
- scope :for_type, lambda{|klass| where(:footprintable_type => klass.name)}
14
- scope :by_type, lambda{|klass| where(:footprinter_type => klass.name)}
8
+ scope :for_type, ->(klass) { where(footprintable_type: klass.name) }
9
+ scope :by_type, ->(klass) { where(footprinter_type: klass.name) }
15
10
 
16
- validates :footprintable_id, :presence => true
17
- validates :footprinter_id, :presence => true
11
+ validates :footprintable_id, presence: true
12
+ validates :footprinter_id, presence: true
18
13
  end
19
- end
14
+ end
@@ -1,10 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActsAsFootprintable
3
4
  module Footprintable
4
-
5
5
  def self.included(base)
6
6
  base.class_eval do
7
- has_many :footprints, :class_name => 'ActsAsFootprintable::Footprint', :as => :footprintable, :dependent => :destroy do
7
+ has_many :footprints, class_name: 'ActsAsFootprintable::Footprint', as: :footprintable, dependent: :destroy do
8
8
  def footprinters
9
9
  includes(:footprinter).map(&:footprinter)
10
10
  end
@@ -13,12 +13,8 @@ module ActsAsFootprintable
13
13
  end
14
14
 
15
15
  def leave_footprints(footprinter)
16
- footprint = ActsAsFootprintable::Footprint.new(:footprintable => self, :footprinter => footprinter)
17
- if footprint.save
18
- true
19
- else
20
- false
21
- end
16
+ footprint = ActsAsFootprintable::Footprint.new(footprintable: self, footprinter: footprinter)
17
+ footprint.save
22
18
  end
23
19
 
24
20
  def footprint_count
@@ -26,7 +22,7 @@ module ActsAsFootprintable
26
22
  end
27
23
 
28
24
  def footprint_count_between(range)
29
- footprints.where(:created_at => range).count
25
+ footprints.where(created_at: range).count
30
26
  end
31
27
  end
32
28
  end
@@ -1,9 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActsAsFootprintable
3
4
  module Footprinter
4
5
  def self.included(base)
5
6
  base.class_eval do
6
- has_many :footprints, :class_name => 'ActsAsFootprintable::Footprint', :as => :footprinter, :dependent => :destroy do
7
+ has_many :footprints, class_name: 'ActsAsFootprintable::Footprint', as: :footprinter, dependent: :destroy do
7
8
  def footprintable
8
9
  includes(:footprintable).map(&:footprintable)
9
10
  end
@@ -12,38 +13,50 @@ module ActsAsFootprintable
12
13
  end
13
14
 
14
15
  def leave_footprints(footprintable)
15
- footprint = ActsAsFootprintable::Footprint.new(:footprintable => footprintable, :footprinter => self)
16
- if footprint.save
17
- true
18
- else
19
- false
20
- end
16
+ footprint = ActsAsFootprintable::Footprint.new(footprintable: footprintable, footprinter: self)
17
+ footprint.save
21
18
  end
22
19
 
23
- def access_histories_for(klass, limit=nil)
20
+ def access_histories_for(klass, limit = nil)
24
21
  get_access_history_records(footprints.for_type(klass), limit)
25
22
  end
26
23
 
27
- def access_histories(limit=nil)
24
+ def access_histories(limit = nil)
28
25
  get_access_history_records(footprints, limit)
29
26
  end
30
27
 
31
28
  private
32
- def get_access_history_records(target, limit=nil)
33
- footprints.where(:id => recent_footprint_ids(target, limit)).order("created_at DESC")
29
+
30
+ def get_access_history_records(target, limit = nil)
31
+ footprints.where(id: recent_footprint_ids(target, limit)).order('created_at DESC')
34
32
  end
35
33
 
36
34
  def table_name
37
35
  ActsAsFootprintable::Footprint.table_name
38
36
  end
39
37
 
40
- def recent_footprint_ids(target, limit=nil)
41
- recent_footprints = target.group("#{table_name}.footprintable_id, #{table_name}.footprintable_type").
42
- select("#{table_name}.footprintable_id, #{table_name}.footprintable_type, MAX(#{table_name}.created_at) AS created_at")
43
- records = footprints.where("(#{table_name}.footprintable_id, #{table_name}.footprintable_type, #{table_name}.created_at) IN (#{recent_footprints.to_sql})")
44
- records = records.order("footprints.created_at desc")
38
+ def recent_footprint_ids(target, limit = nil)
39
+ records = footprints.where(<<~SQL)
40
+ (
41
+ #{table_name}.footprintable_id,
42
+ #{table_name}.footprintable_type,
43
+ #{table_name}.created_at
44
+ ) IN (#{recent_footprints_by(target).to_sql})
45
+ SQL
46
+ records = records.order('footprints.created_at desc')
45
47
  records = records.limit(limit) unless limit.nil?
46
- records.pluck(:id)
48
+ records.ids
49
+ end
50
+
51
+ def recent_footprints_by(target)
52
+ target.group(
53
+ "#{table_name}.footprintable_id",
54
+ "#{table_name}.footprintable_type"
55
+ ).select(
56
+ "#{table_name}.footprintable_id",
57
+ "#{table_name}.footprintable_type",
58
+ "MAX(#{table_name}.created_at) AS created_at"
59
+ )
47
60
  end
48
61
  end
49
- end
62
+ end
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module ActsAsFootprintable
3
- VERSION = "0.4.0"
4
+ VERSION = '0.6.2'
4
5
  end
@@ -1,4 +1,5 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'active_record'
3
4
  require 'active_support/inflector'
4
5
 
@@ -1,32 +1,43 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'rails/generators/migration'
3
4
 
4
5
  module ActsAsFootprintable
5
6
  class MigrationGenerator < Rails::Generators::Base
6
7
  include Rails::Generators::Migration
7
8
 
8
- desc "Generators migration for footprintable(footprints table)"
9
+ desc 'Generators migration for footprintable(footprints table)'
9
10
 
10
11
  def self.orm
11
12
  Rails::Generators.options[:rails][:orm]
12
13
  end
13
14
 
14
15
  def self.source_root
15
- File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)))
16
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.instance_of?(String)))
16
17
  end
17
18
 
18
19
  def self.orm_has_migration?
19
20
  [:active_record].include? orm
20
21
  end
21
22
 
22
- def self.next_migration_number(path)
23
- Time.now.utc.strftime("%Y%m%d%H%M%S")
23
+ def self.next_migration_number(_path)
24
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
24
25
  end
25
26
 
26
27
  def create_migration_file
27
- if self.class.orm_has_migration?
28
- migration_template 'migration.rb', 'db/migrate/acts_as_footprintable_migration'
29
- end
28
+ return unless self.class.orm_has_migration?
29
+
30
+ migration_template 'migration.rb',
31
+ 'db/migrate/acts_as_footprintable_migration.rb',
32
+ migration_version: migration_version
33
+ end
34
+
35
+ def migration_version
36
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if over_rails5?
37
+ end
38
+
39
+ def over_rails5?
40
+ Rails::VERSION::MAJOR >= 5
30
41
  end
31
42
  end
32
- end
43
+ end
@@ -1,10 +1,11 @@
1
- # coding: utf-8
2
- class ActsAsFootprintableMigration < ActiveRecord::Migration
1
+ # frozen_string_literal: true
2
+
3
+ class ActsAsFootprintableMigration < ActiveRecord::Migration<%= migration_version %>
3
4
  def self.up
4
5
  create_table :footprints do |t|
5
- t.references :footprintable, :polymorphic => true
6
- t.references :footprinter, :polymorphic => true
7
- t.timestamps :null => false
6
+ t.references :footprintable, polymorphic: true
7
+ t.references :footprinter, polymorphic: true
8
+ t.timestamps null: false
8
9
  end
9
10
 
10
11
  if ActiveRecord::VERSION::MAJOR < 4