views_count 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6eccaf5cb6ddabc13f1af49806569dbcc265b3c5
4
+ data.tar.gz: 49ea063b00f93a4c6d95cf31694b54f8c5227bd2
5
+ SHA512:
6
+ metadata.gz: 299ab7d8e404d64e4bc8745865ceeef1a957252409db825bdfe50d97e94cebde11b8d95b7635c126700218fe859860bbf6b9db36432d5c6146fd22c1a33221db
7
+ data.tar.gz: ecf72a2fb3eb456ea7687fe5d4e1ac3ade88fb75aa4ad59b40c3d73237b1b6837f2657aa544bd66c4db78c8d529d310b5cda193fc08862f1990c969627ec5c39
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in views_count.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ben Zhang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ViewsCount
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'views_count'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install views_count
18
+
19
+ Generate the config file and table migration
20
+
21
+ $ rails g views_count
22
+
23
+ This allows you to attach views count method to an AR model instance. By default,
24
+ it requires a column views_count to keep track of the view hits. You can specify column_name to whatever fits your situation.
25
+
26
+ class Topic < ActiveRecord::Base
27
+ has_views_count
28
+ end
29
+
30
+ class Topic < ActiveRecord::Base
31
+ has_views_count column_name: 'views_count'
32
+ end
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/[my-github-username]/views_count/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,18 @@
1
+ class CreateViewItemsTable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :view_items, force: true do |t|
4
+ t.string :target_type
5
+ t.integer :target_id
6
+ t.integer :user_id
7
+ t.string :ip_address
8
+ t.datetime :viewed_on
9
+ end
10
+
11
+ add_index :view_items, [:target_type, :target_id, :user_id], unique: true
12
+ add_index :view_items, [:target_type, :target_id, :ip_address], unique: true
13
+ end
14
+
15
+ def self.down
16
+ drop_table :view_items
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module ActiveRecord
2
+ module Generators
3
+ class ViewsCountGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+ source_root File.join(File.dirname(__FILE__), 'templates')
6
+
7
+ def self.next_migration_number(dirname)
8
+ if ActiveRecord::Base.timestamped_migrations
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
10
+ else
11
+ "%.3d" % (current_migration_number(dirname) + 1)
12
+ end
13
+ end
14
+
15
+ def create_migration_file
16
+ migration_template 'create_view_items_table.rb', 'db/migrate/create_view_items_table.rb'
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ # ViewsCount.setup do |config|
2
+ # Define time frame
3
+ # config.time_frame = 24.hours
4
+ # end
@@ -0,0 +1,13 @@
1
+ module ViewsCount
2
+ module Generators
3
+ class ViewsCountGenerator < Rails::Generators::Base
4
+ hook_for :orm
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def copy_config_file
8
+ template 'views_count.rb', 'config/initializers/views_count.rb'
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module ViewsCount
2
+ class ViewItem < ActiveRecord::Base
3
+ belongs_to :target, polymorphic: true
4
+
5
+ if respond_to?(:attr_accessible)
6
+ attr_accessible :user_id, :viewed_on, :ip_address
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ module ViewsCount
2
+ module ViewsCountable
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def has_views_count(options = {})
8
+ has_many :view_items, as: :target, dependent: :destroy, class_name: 'ViewsCount::ViewItem'
9
+ @@views_count_options = {
10
+ counter_cache: true,
11
+ column_name: :views_count,
12
+ unique: true
13
+ }.merge(options)
14
+
15
+ class_eval do
16
+ def add_views_count(request, user = nil)
17
+ raise ArgumentError if request.nil? && user.nil?
18
+
19
+ view_item = if user.nil?
20
+ self.view_items.where(ip_address: request.remote_ip).last
21
+ else
22
+ self.view_items.where(user_id: user.id).last
23
+ end
24
+
25
+ return if view_item and view_item.viewed_on >= ViewsCount.time_frame.ago
26
+
27
+ if view_item
28
+ view_item.viewed_on = Time.now
29
+ else
30
+ view_item = self.view_items.build(ip_address: request.remote_ip, user_id: user.try(:id), viewed_on: Time.now)
31
+ end
32
+
33
+ begin
34
+ transaction do
35
+ view_item.save!
36
+ self.class.update_all("#{@@views_count_options[:column_name]} = COALESCE(\"#{@@views_count_options[:column_name]}\", 0) + 1", "id = #{id}")
37
+ self.reload
38
+ end
39
+ rescue ActiveRecord::RecordNotUnique
40
+ rescue ActiveRecord::RecordInvalid
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/railtie'
2
+
3
+ module ViewsCount
4
+ class Railtie < Rails::Railtie
5
+
6
+ initializer 'views_count.model' do |app|
7
+ require "models/views_count/views_countable.rb"
8
+ require "models/views_count/view_item.rb"
9
+ ActiveRecord::Base.send(:include, ViewsCount::ViewsCountable)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ViewsCount
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require "views_count/version"
2
+ require "views_count/railtie"
3
+
4
+ module ViewsCount
5
+
6
+ mattr_accessor :time_frame
7
+ @@time_frame = 24.hours
8
+
9
+ # Load configuration from initializer
10
+ def self.setup
11
+ yield self
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'views_count/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "views_count"
8
+ spec.version = ViewsCount::VERSION
9
+ spec.authors = ["Ben Zhang"]
10
+ spec.email = ["bzbnhang@gmail.com"]
11
+ spec.summary = %q{ViewsCount is a lightweight plugin to keep track of views of an ActiveRecord model.}
12
+ spec.description = %q{ViewsCount is a lightweight plugin to keep track of views of an ActiveRecord model.}
13
+ spec.homepage = "https://github.com/BenZhang/views_count"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: views_count
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: ViewsCount is a lightweight plugin to keep track of views of an ActiveRecord
42
+ model.
43
+ email:
44
+ - bzbnhang@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/generators/active_record/templates/create_view_items_table.rb
55
+ - lib/generators/active_record/views_count_generator.rb
56
+ - lib/generators/templates/views_count.rb
57
+ - lib/generators/views_count_generator.rb
58
+ - lib/models/views_count/view_item.rb
59
+ - lib/models/views_count/views_countable.rb
60
+ - lib/views_count.rb
61
+ - lib/views_count/railtie.rb
62
+ - lib/views_count/version.rb
63
+ - views_count.gemspec
64
+ homepage: https://github.com/BenZhang/views_count
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: ViewsCount is a lightweight plugin to keep track of views of an ActiveRecord
88
+ model.
89
+ test_files: []