popularable 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +41 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +7 -0
- data/README.md +19 -0
- data/lib/concerns/popularable.rb +32 -0
- data/lib/generators/popularable/install_generator.rb +40 -0
- data/lib/models/popularable_popularity_event.rb +4 -0
- data/lib/popularable.rb +15 -0
- data/lib/popularable/engine.rb +6 -0
- data/lib/popularable/popularable.rb +2 -0
- data/lib/popularable/version.rb +5 -0
- data/popularable.gemspec +26 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87cb950ce8fbcb244d3331dcf656e052dceeaaea
|
4
|
+
data.tar.gz: 26d563f2c0906f4972abd6b47d2e50f3474ed701
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 777d98c936990f73114222162de22048daa5525c18171cbfe2aae061d4191d03077308078e4be1312058d150b2d228bdfd6fbb8aef2a8ee8207fe4c57827fd68
|
7
|
+
data.tar.gz: 94997b2e73c3cee9c47981d448f1484f124ea5bc97544475939a8acd34182f44b03e071f884c3ca418b3dcf02ef5df27ba696d852a7c0bcd763486196bf5ede3
|
data/.gitignore
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
# Ignore bundler config.
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore the default SQLite database.
|
11
|
+
/db/*.sqlite3
|
12
|
+
/db/*.sqlite3-journal
|
13
|
+
|
14
|
+
# Ignore all logfiles and tempfiles.
|
15
|
+
/log/*
|
16
|
+
!/log/.keep
|
17
|
+
/tmp
|
18
|
+
|
19
|
+
|
20
|
+
/solr/data/*
|
21
|
+
/solr/development/*
|
22
|
+
/solr/default/data/*
|
23
|
+
/solr/pids/*
|
24
|
+
sunspot-solr.pid
|
25
|
+
|
26
|
+
*.rbc
|
27
|
+
*.sassc
|
28
|
+
.sass-cache
|
29
|
+
capybara-*.html
|
30
|
+
/vendor/bundle
|
31
|
+
/public/system/*
|
32
|
+
/coverage/
|
33
|
+
/spec/tmp/*
|
34
|
+
**.orig
|
35
|
+
rerun.txt
|
36
|
+
pickle-email-*.html
|
37
|
+
.DS_Store
|
38
|
+
*.swp
|
39
|
+
|
40
|
+
# Ignore application configuration
|
41
|
+
/config/application.yml
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2015 Jeff McFadden
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# popularable
|
2
|
+
|
3
|
+
Add management for ordering your models by a historical popularity value.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem 'popularable'
|
8
|
+
|
9
|
+
$ bundle install
|
10
|
+
$ bundle exec rails g popularable:install
|
11
|
+
$ bundle exec rake db:migrate
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
class Widget < ActiveRecord::Base
|
16
|
+
include Popularable::Concern
|
17
|
+
|
18
|
+
|
19
|
+
widget.first.bump_popularity!
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Popularable
|
2
|
+
module Concern
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
# scope :order_by_recent_popularity, -> { .joins( "LEFT OUTER JOIN popularable_popularity_events ON (" + self.to_s.pluralize.underscore + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "')").where( ["popularable_popularity_events.popularity_event_date >= ?", 1.month.ago] ).group( self.to_s.pluralize.underscore + ".id" ).order( "popularity DESC" ) }
|
7
|
+
|
8
|
+
scope :popular_today, -> { find_by_sql( ["SELECT " + self.table_name + ".*, 0 + SUM(popularable_popularity_events.popularity) AS popularity FROM " + self.table_name + " LEFT OUTER JOIN popularable_popularity_events ON (" + self.table_name + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "') WHERE popularable_popularity_events.popularity_event_date >= ? GROUP BY " + self.table_name + ".id ORDER BY popularity DESC", Time.now.to_date] ) }
|
9
|
+
|
10
|
+
scope :popular_this_week, -> { find_by_sql( ["SELECT " + self.table_name + ".*, 0 + SUM(popularable_popularity_events.popularity) AS popularity FROM " + self.table_name + " LEFT OUTER JOIN popularable_popularity_events ON (" + self.table_name + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "') WHERE popularable_popularity_events.popularity_event_date >= ? GROUP BY " + self.table_name + ".id ORDER BY popularity DESC", Time.now.beginning_of_week.to_date] ) }
|
11
|
+
|
12
|
+
scope :popular_this_month, -> { find_by_sql( ["SELECT " + self.table_name + ".*, 0 + SUM(popularable_popularity_events.popularity) AS popularity FROM " + self.table_name + " LEFT OUTER JOIN popularable_popularity_events ON (" + self.table_name + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "') WHERE popularable_popularity_events.popularity_event_date >= ? GROUP BY " + self.table_name + ".id ORDER BY popularity DESC", Time.now.beginning_of_month.to_date] ) }
|
13
|
+
|
14
|
+
scope :popular_this_year, -> { find_by_sql( ["SELECT " + self.table_name + ".*, 0 + SUM(popularable_popularity_events.popularity) AS popularity FROM " + self.table_name + " LEFT OUTER JOIN popularable_popularity_events ON (" + self.table_name + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "') WHERE popularable_popularity_events.popularity_event_date >= ? GROUP BY " + self.table_name + ".id ORDER BY popularity DESC", Time.now.beginning_of_year.to_date] ) }
|
15
|
+
|
16
|
+
scope :popular_all_time, -> { find_by_sql( ["SELECT " + self.table_name + ".*, 0 + SUM(popularable_popularity_events.popularity) AS popularity FROM " + self.table_name + " LEFT OUTER JOIN popularable_popularity_events ON (" + self.table_name + ".id = popularable_popularity_events.popularable_id AND popularable_popularity_events.popularable_type = '" + self.to_s + "') GROUP BY " + self.table_name + ".id ORDER BY popularity DESC"] ) }
|
17
|
+
|
18
|
+
has_many :popularable_popularity_events, as: :popularable
|
19
|
+
|
20
|
+
def self.has_popularable_concern?
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def bump_popularity!( popularity_add_value = 1, popularity_event_time = Time.now )
|
27
|
+
popularable_popularity_event = self.popularable_popularity_events.find_or_create_by( popularity_event_date: popularity_event_time.to_date )
|
28
|
+
|
29
|
+
popularable_popularity_event.update_attributes( popularity: popularable_popularity_event.popularity.to_i + popularity_add_value )
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Popularable
|
4
|
+
class InstallGenerator < ::Rails::Generators::Base
|
5
|
+
desc "Install a migration for the popularable gem"
|
6
|
+
source_root File.expand_path('../generators', __FILE__)
|
7
|
+
|
8
|
+
def create_migration_file
|
9
|
+
destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_popularable_installation_migration.rb".gsub(" ", "")
|
10
|
+
migration_name = "PopularableInstallationMigration".gsub(" ", "")
|
11
|
+
count = nil
|
12
|
+
i = 1
|
13
|
+
while !Dir.glob("db/migrate/*_popularable_installation_migration.rb".gsub(" ", "")).empty?
|
14
|
+
i += 1
|
15
|
+
count = "_#{i}"
|
16
|
+
destination = "db/migrate/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_popularable_installation_migration.rb".gsub(" ", "")
|
17
|
+
migration_name = "PopularableInstallationMigration#{i}".gsub(" ", "")
|
18
|
+
end
|
19
|
+
create_file destination, <<-FILE
|
20
|
+
class #{migration_name} < ActiveRecord::Migration
|
21
|
+
def change
|
22
|
+
create_table :popularable_popularity_events do |t|
|
23
|
+
t.string :popularable_type
|
24
|
+
t.integer :popularable_id
|
25
|
+
|
26
|
+
t.date :popularity_event_date
|
27
|
+
t.integer :popularity, default: 0
|
28
|
+
|
29
|
+
t.timestamps null: false
|
30
|
+
end
|
31
|
+
|
32
|
+
add_index :popularable_popularity_events, [:popularable_type, :popularable_id], name: "index_popularable_popularity_events_type_id"
|
33
|
+
add_index :popularable_popularity_events, [:popularity_event_date, :popularable_type, :popularable_id, :popularity], name: "index_popularable_popularity_events_type_id_popularity"
|
34
|
+
add_index :popularable_popularity_events, :popularity
|
35
|
+
end
|
36
|
+
end
|
37
|
+
FILE
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/popularable.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'concerns/popularable'
|
3
|
+
require 'popularable/popularable'
|
4
|
+
require 'models/popularable_popularity_event'
|
5
|
+
require 'popularable/engine'
|
6
|
+
require 'popularable/version'
|
7
|
+
|
8
|
+
# ActiveSupport.on_load(:active_record) do
|
9
|
+
# include Trendable::Manager
|
10
|
+
# end
|
11
|
+
|
12
|
+
module Popularable
|
13
|
+
class Manager
|
14
|
+
end
|
15
|
+
end
|
data/popularable.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/popularable/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "popularable"
|
6
|
+
s.version = Popularable::Rails::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
|
9
|
+
s.authors = ["Jeff McFadden"]
|
10
|
+
s.date = "2015-08-27"
|
11
|
+
s.description = "Organize your models by a historical popularity value"
|
12
|
+
s.email = "jeff@forgeapps.com"
|
13
|
+
s.extra_rdoc_files = [
|
14
|
+
"LICENSE.txt"
|
15
|
+
]
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
|
18
|
+
s.homepage = "http://github.com/ForgeApps/popularable"
|
19
|
+
s.licenses = ["MIT"]
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.rubygems_version = Popularable::Rails::VERSION
|
22
|
+
s.summary = "Organize your models by a historical popularity value"
|
23
|
+
|
24
|
+
s.add_dependency "activesupport", ">= 4.1.0"
|
25
|
+
s.add_dependency "sidekiq"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: popularable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff McFadden
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sidekiq
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Organize your models by a historical popularity value
|
42
|
+
email: jeff@forgeapps.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files:
|
46
|
+
- LICENSE.txt
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- lib/concerns/popularable.rb
|
53
|
+
- lib/generators/popularable/install_generator.rb
|
54
|
+
- lib/models/popularable_popularity_event.rb
|
55
|
+
- lib/popularable.rb
|
56
|
+
- lib/popularable/engine.rb
|
57
|
+
- lib/popularable/popularable.rb
|
58
|
+
- lib/popularable/version.rb
|
59
|
+
- popularable.gemspec
|
60
|
+
homepage: http://github.com/ForgeApps/popularable
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.5.1
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Organize your models by a historical popularity value
|
84
|
+
test_files: []
|