acts_as_viewable 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Michael Bleigh and Intridea Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,29 @@
1
+ Insallation
2
+ +++++++++++++
3
+
4
+ sudo gem install acts_as_viewable
5
+
6
+
7
+
8
+ Post Installation (Rails)
9
+ -------------------------
10
+ 1. script/generate acts_as_viewable_on_migration
11
+ 2. rake db:migrate
12
+
13
+
14
+
15
+ Example
16
+ =======
17
+
18
+ class Property < ActiveRecord::Base
19
+ acts_as_viewable_on :authorized, :unuthorized, :managers
20
+ end
21
+
22
+ @property = Property.new(:name => "Bobby")
23
+ @property.increase_views_on_managers
24
+ @property.views_on_manager # => 1
25
+ @property.views_on_authorized # => 0
26
+ @property.events_on_managers
27
+ # => {:created_at => "12.23.2009" , :ip => '192.168.1.1' , :user_agent => 'agent' , :os => 'Windows'}
28
+
29
+
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "acts_as_viewable"
5
+ gemspec.summary = "Its provide flexibility sustem for calculate counts of page view"
6
+ gemspec.email = "dieinzige@gmail.com"
7
+ gemspec.homepage = "http://github.com/dieinzige/acts_as_viewable"
8
+ gemspec.authors = ["Dieinzige"]
9
+ gemspec.files = FileList["[A-Z]*", "{lib,rails}/**/*"] - FileList["**/*.log"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
14
+ end
15
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,3 @@
1
+ require 'acts_as_viewable/acts_as_viewable'
2
+ require 'acts_as_viewable/view'
3
+ require 'acts_as_viewable/view_event'
@@ -0,0 +1,75 @@
1
+ module ActsAsViewable
2
+ module ClassMethods
3
+ def acts_as_viewable
4
+ acts_as_viewable {:all => 100}
5
+ end
6
+
7
+ def acts_as_viewable(*args)
8
+ args.each do |k,v|
9
+ context = k.singularize
10
+ self.class_eval <<-RUBY
11
+ def increase_views_on_#{context}(request=nil)
12
+ increase_for("#{context}","#{v}",request)
13
+ end
14
+
15
+ def views_on_#{context}
16
+ views_on("#{context}")
17
+ end
18
+
19
+ def events_on_#{context}
20
+ events_on("#{context}")
21
+ end
22
+
23
+ RUBY
24
+ end
25
+ end
26
+ end
27
+
28
+ module InstanceMethods
29
+ def views_on(context)
30
+ Rails.cache.fetch("#{self.class}_#{context}_count") do
31
+ view=View.first :conditions => {:context => context , :viewable_id => self.id , :viewable_type => self.class.to_s }
32
+ view ? view.quantity : 0
33
+ end
34
+ end
35
+
36
+ def events_on(context)
37
+ ViewEvent.all :conditions => {:context => context , :viewable_id => self.id , :viewable_type => self.class.to_s }
38
+ end
39
+
40
+ def increase_for(context, persis_count = 100, request)
41
+ if views_on(context) % persis_count == 0
42
+ non_cache_increase(context,request)
43
+ else
44
+ cache_increase(context,request)
45
+ end
46
+ end
47
+
48
+ def non_cache_increase(context,request)
49
+ count = Rails.cache.fetch("#{self.class}_#{context}_count")
50
+ view_item = View.first :conditions => {:context => context , :viewable_id => self.id , :viewable_type => self.class.to_s }
51
+ unless view_item
52
+ view_item = View.new({:viewable_id => self.id , :viewable_type => self.class.to_s, :quantity => count})
53
+ end
54
+ view_item.quantity = count
55
+ view_item.save
56
+ views = Rails.cache.read("#{self.class}_#{context}_views") || []
57
+ views.each do |k|
58
+ view_item.create_view_event(k.first)
59
+ end
60
+ end
61
+
62
+ def cache_increase(context,request)
63
+ views = views_on(context)
64
+ Rails.cache.write("#{self.class}_#{context}_count",views+1)
65
+ views = Rails.cache.read("#{self.class}_#{context}_views") || []
66
+ views << {:ip => request.remote_ip , :user_agent => request.env['HTTP_USER_AGENT']}
67
+ Rails.cache.write("#{self.class}_#{context}_views",views)
68
+ end
69
+ end
70
+
71
+ def self.included(receiver)
72
+ receiver.extend ClassMethods
73
+ receiver.send :include, InstanceMethods
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ class View < ActiveRecord::Base
2
+ belongs_to :viewable, :polymorphic => true
3
+ has_many :view_events, :class_name => "view_event", :foreign_key => "reference_id"
4
+ validates_presence_of :context
5
+ alias_method :events , :view_events
6
+ end
@@ -0,0 +1,3 @@
1
+ class ViewEvent < ActiveRecord::Base
2
+ belongs_to :view
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'acts_as_viewable'
2
+
3
+ ActiveRecord::Base.send :include, ActsAsViewable
4
+
5
+ RAILS_DEFAULT_LOGGER.info "** acts_as_viewable init correctly"
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_viewable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dieinzige
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-22 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dieinzige@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - CHANGELOG
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - VERSION
30
+ - lib/acts_as_viewable.rb
31
+ - lib/acts_as_viewable/acts_as_viewable.rb
32
+ - lib/acts_as_viewable/view.rb
33
+ - lib/acts_as_viewable/view_event.rb
34
+ - rails/init.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/dieinzige/acts_as_viewable
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Its provide flexibility sustem for calculate counts of page view
63
+ test_files: []
64
+