santey_view 0.1.0

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Alexander Shamne
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.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = Rails santey_view plugin based on acts_as_viewed
2
+
3
+ rails generate santey_view
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 Alexander Shamne. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "santey_view"
8
+ gem.summary = %Q{Views count gem}
9
+ gem.description = %Q{longer description of your gem}
10
+ gem.email = "alexander.shamne@gmail.com"
11
+ gem.homepage = "http://github.com/shamne/santey_view"
12
+ gem.authors = ["Alexander Shamne"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "santey_view #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+
3
+ class SanteyViewGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
+ end
9
+
10
+ def self.next_migration_number(dirname)
11
+ Time.now.strftime("%Y%m%d%H%M%S")
12
+ end
13
+
14
+ # copy migrations to db/migrate
15
+ def install_migrations
16
+ migration_template "migrations/viewings.rb", File.join('db/migrate', "viewings.rb")
17
+ end
18
+
19
+ end
@@ -0,0 +1,18 @@
1
+ class Viewings < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :viewings do |t|
4
+ t.column :viewer_id, :integer
5
+ t.column :viewed_id, :integer
6
+ t.column :viewed_type, :string
7
+ t.column :ip, :string, :limit => '24'
8
+ t.column :created_at, :datetime
9
+ end
10
+
11
+ add_index :viewings, [:viewer_id], :name => "index_viewings"
12
+ add_index :viewings, [:viewed_type, :viewed_id], :name => "index_viewings_viewed"
13
+ end
14
+
15
+ def self.down
16
+ drop_table :viewings
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module SanteyView
2
+
3
+ autoload :Viewable, 'santey_view/viewable'
4
+
5
+ end
6
+
7
+ require 'santey_view/engine'
@@ -0,0 +1,20 @@
1
+ require "active_record"
2
+
3
+ require "santey_view"
4
+ require "rails"
5
+
6
+
7
+ module SanteyView
8
+ class Engine < Rails::Engine
9
+
10
+ #config.after_initialize do
11
+ # ActiveRecord::Base.send :include, SanteyView::Viewable
12
+ #end
13
+
14
+ end
15
+ end
16
+
17
+
18
+ if defined?(ActiveRecord::Base)
19
+ ActiveRecord::Base.send :include, SanteyView::Viewable
20
+ end
@@ -0,0 +1,165 @@
1
+ module SanteyView
2
+ module Viewable
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def santey_view(options = {})
11
+ # don't allow multiple calls
12
+ return if self.included_modules.include?(SanteyView::Viewable::ViewMethods)
13
+ include SanteyView::Viewable::ViewMethods
14
+
15
+ viewing_class = options[:viewing_class] || 'Viewing'
16
+ viewer_class = options[:viewer_class] || 'User'
17
+
18
+ unless Object.const_defined?(viewing_class)
19
+ Object.class_eval <<-EOV
20
+ class #{viewing_class} < ActiveRecord::Base
21
+ belongs_to :viewed, :polymorphic => true
22
+ end
23
+ EOV
24
+ if Object.const_defined?(viewer_class)
25
+ Object.class_eval <<-EOV
26
+ class #{viewing_class} < ActiveRecord::Base
27
+ belongs_to :viewer, :class_name => #{viewer_class}, :foreign_key => :viewer_id
28
+ end
29
+ EOV
30
+ end
31
+ end
32
+
33
+ write_inheritable_attribute( :santey_view_options ,
34
+ { :viewing_class => viewing_class,
35
+ :viewer_class => viewer_class } )
36
+ class_inheritable_reader :santey_view_options
37
+
38
+ class_eval do
39
+ has_many :viewings, :as => :viewed, :dependent => :delete_all, :class_name => viewing_class.to_s
40
+ if Object.const_defined?(viewer_class)
41
+ has_many(:viewers, :through => :viewings, :class_name => viewer_class.to_s)
42
+ end
43
+ before_create :init_viewing_fields
44
+ end
45
+
46
+ # Add to the Viewer a has_many viewings
47
+ if Object.const_defined?(viewer_class)
48
+ viewer_as_class = viewer_class.constantize
49
+ return if viewer_as_class.instance_methods.include?('find_in_viewings')
50
+ viewer_as_class.class_eval <<-EOS
51
+ has_many :viewings, :foreign_key => :viewer_id, :class_name => #{viewing_class.to_s}
52
+ EOS
53
+ end
54
+ end
55
+
56
+
57
+
58
+
59
+ def generate_viewings_columns table
60
+ table.column :views, :integer
61
+ end
62
+
63
+ # Create the needed columns for santey_view.
64
+ # To be used during migration, but can also be used in other places.
65
+ def add_viewings_columns
66
+ if !self.content_columns.find { |c| 'views' == c.name }
67
+ self.connection.add_column table_name, :views, :integer, :default => '0'
68
+ self.reset_column_information
69
+ end
70
+ end
71
+
72
+ # Remove the santey_view specific columns added with add_viewings_columns
73
+ # To be used during migration, but can also be used in other places
74
+ def remove_viewings_columns
75
+ if self.content_columns.find { |c| 'views' == c.name }
76
+ self.connection.remove_column table_name, :views
77
+ self.reset_column_information
78
+ end
79
+ end
80
+
81
+ # Find all viewings for a specific viewer.
82
+ def find_viewed_by viewer
83
+ viewing_class = santey_view_options[:viewing_class].constantize
84
+ if !(santey_view_options[:viewer_class].constantize === viewer)
85
+ raise ViewedError, "The viewer object must be the one used when defining santey_view (or a descendent of it). other objects are not acceptable"
86
+ end
87
+ raise ViewedError, "Viewer must be a valid and existing object" if viewer.nil? || viewer.id.nil?
88
+ raise ViewedError, 'Viewer must be a valid viewer' if !viewing_class.column_names.include? "viewer_id"
89
+ viewed_class = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
90
+ conds = [ 'viewed_type = ? AND viewer_id = ?', viewed_class, viewer.id ]
91
+ santey_view_options[:viewing_class].constantize.find(:all, :conditions => conds).collect {|r| r.viewed_type.constantize.find_by_id r.viewed.id }
92
+ end
93
+ end
94
+
95
+ module ViewMethods
96
+
97
+ def self.included(base) #:nodoc:
98
+ base.extend ClassMethods
99
+ end
100
+
101
+ # Is this object viewed already?
102
+ def viewed?
103
+ return (!self.views.nil? && self.views > 0) if attributes.has_key? 'views'
104
+ !viewings.find(:first).nil?
105
+ end
106
+
107
+ # Get the number of viewings for this object based on the views field,
108
+ # or with a SQL query if the viewed objects doesn't have the views field
109
+ def view_count
110
+ return self.views || 0 if attributes.has_key? 'views'
111
+ viewings.count
112
+ end
113
+
114
+ # View the object with or without a viewer - create new or update as needed
115
+ #
116
+ # * <tt>ip</tt> - the viewer ip
117
+ # * <tt>viewer</tt> - an object of the viewer class. Must be valid and with an id to be used. Or nil
118
+ def view ip, viewer = nil
119
+ # Sanity checks for the parameters
120
+ viewing_class = santey_view_options[:viewing_class].constantize
121
+ if viewer && !(santey_view_options[:viewer_class].constantize === viewer)
122
+ raise ViewedError, "the viewer object must be the one used when defining santey_view (or a descendent of it). other objects are not acceptable"
123
+ end
124
+
125
+ viewing_class.transaction do
126
+ if !viewed_by? ip, viewer
127
+ view = viewing_class.new
128
+ view.viewer_id = viewer.id if viewer && !viewer.id.nil?
129
+ view.ip = ip
130
+ viewings << view
131
+ target = self if attributes.has_key? 'views'
132
+ target.views = ( (target.views || 0) + 1 ) if target
133
+ view.save
134
+ target.save_without_validation if target
135
+ return true
136
+ else
137
+ return false
138
+ end
139
+ end
140
+ end
141
+
142
+ # Check if an item was already viewed by the given viewer
143
+ def viewed_by? ip, viewer = nil
144
+ if viewer && !viewer.nil? && !(santey_view_options[:viewer_class].constantize === viewer)
145
+ raise ViewedError, "the viewer object must be the one used when defining santey_view (or a descendent of it). other objects are not acceptable"
146
+ end
147
+ if viewer && !viewer.id.nil?
148
+ return viewings.count(:conditions => ["viewer_id = '#{viewer.id}' or ip = '#{ip}'"]) > 0
149
+ else
150
+ return viewings.count(:conditions => ["ip = '#{ip}'"]) > 0
151
+ end
152
+ end
153
+
154
+ private
155
+
156
+ def init_viewing_fields #:nodoc:
157
+ if attributes.has_key? 'views'
158
+ self.views ||= 0
159
+ end
160
+ end
161
+
162
+ end
163
+
164
+ end
165
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{santey_view}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Alexander Shamne"]
12
+ s.date = %q{2010-06-20}
13
+ s.description = %q{longer description of your gem}
14
+ s.email = %q{alexander.shamne@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/generators/santey_view_generator.rb",
27
+ "lib/generators/templates/migrations/viewings.rb",
28
+ "lib/santey_view.rb",
29
+ "lib/santey_view/engine.rb",
30
+ "lib/santey_view/viewable.rb",
31
+ "santey_view.gemspec",
32
+ "test/santey_view_test.rb",
33
+ "test/test_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/shamne/santey_view}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{Views count gem}
40
+ s.test_files = [
41
+ "test/santey_view_test.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class SanteyViewTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'santey_view'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: santey_view
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Alexander Shamne
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-20 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: longer description of your gem
33
+ email: alexander.shamne@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - lib/generators/santey_view_generator.rb
49
+ - lib/generators/templates/migrations/viewings.rb
50
+ - lib/santey_view.rb
51
+ - lib/santey_view/engine.rb
52
+ - lib/santey_view/viewable.rb
53
+ - santey_view.gemspec
54
+ - test/santey_view_test.rb
55
+ - test/test_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/shamne/santey_view
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.6
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Views count gem
86
+ test_files:
87
+ - test/santey_view_test.rb
88
+ - test/test_helper.rb