jcnetdev-acts-as-readable 1.0.200807042

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/README ADDED
@@ -0,0 +1,54 @@
1
+ ActsAsReadable
2
+ ==============
3
+
4
+ ActsAsReadable allows you to create a generic relationship of items which can
5
+ be marked as 'read' by users. This is useful for forums or any other kind of
6
+ situation where you might need to know whether or not a user has seen a particular
7
+ model.
8
+
9
+ Installation
10
+ ============
11
+
12
+ To install the plugin from git:
13
+
14
+ script/plugin install git://github.com/jcnetdev/acts-as-readable.git
15
+
16
+ To install the plugin from a gem, add this to our environment.rb:
17
+
18
+ config.gem 'jcnetdev-acts-as-readable', :version => '>= 1.0',
19
+ :lib => 'acts_as_readable',
20
+ :source => 'http://gems.github.com'
21
+
22
+
23
+ You will need the readings table to use this plugin. A generator has been included,
24
+ simply type
25
+
26
+ script/generate acts_as_readable_migration
27
+
28
+ to get the standard migration created for you.
29
+
30
+ Example
31
+ =======
32
+
33
+ class Post < ActiveRecord::Base
34
+ acts_as_readable
35
+ end
36
+
37
+ bob = User.find_by_name("bob")
38
+
39
+ bob.readings # => []
40
+
41
+ Post.find_unread_by(bob) # => [<Post 1>,<Post 2>,<Post 3>...]
42
+ Post.find_read_by(bob) # => []
43
+
44
+ Post.find(1).read_by?(bob) # => false
45
+ Post.find(1).read_by!(bob) # => <Reading 1>
46
+ Post.find(1).read_by?(bob) # => true
47
+ Post.find(1).users_who_read # => [<User bob>]
48
+
49
+ Post.find_unread_by(bob) # => [<Post 2>,<Post 3>...]
50
+ Post.find_read_by(bob) # => [<Post 1>]
51
+
52
+ bob.readings # => [<Reading 1>]
53
+
54
+ Copyright (c) 2008 Michael Bleigh and Intridea, Inc. released under the MIT license
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'acts-as-readable'
3
+ s.version = '1.0.200807042'
4
+ s.date = '2008-07-04'
5
+
6
+ s.summary = "Makes calling partials in views look better and more fun."
7
+ s.description = "Wrapper around render :partial that removes the need to use :locals, and allows blocks to be taken easily"
8
+
9
+ s.authors = ['Michael Bleigh']
10
+ s.email = 'michael@intridea.com'
11
+ s.homepage = 'http://github.com/mbleigh/acts-as-readable'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+ s.files = ["README",
20
+ "acts-as-readable.gemspec",
21
+ "generators/acts_as_readable_migration/acts_as_readable_migration_generator.rb",
22
+ "generators/acts_as_readable_migration/templates/migration.rb",
23
+ "init.rb",
24
+ "lib/acts-as-readable.rb",
25
+ "lib/reading.rb",
26
+ "lib/user_with_readings.rb",
27
+ "rails/init.rb"]
28
+
29
+ end
30
+
@@ -0,0 +1,11 @@
1
+ class ActsAsReadableMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate'
5
+ end
6
+ end
7
+
8
+ def file_name
9
+ "acts_as_readable_migration"
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class ActsAsReadableMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :readings do |t|
4
+ t.string :readable_type
5
+ t.integer :readable_id
6
+ t.integer :user_id
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :readings
13
+ end
14
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,43 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Readable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_readable
10
+ has_many :readings, :as => :readable
11
+ has_many :users_who_read, :through => :readings, :source => :user
12
+
13
+ include ActiveRecord::Acts::Readable::InstanceMethods
14
+ extend ActiveRecord::Acts::Readable::SingletonMethods
15
+ end
16
+ end
17
+
18
+ module SingletonMethods
19
+ def find_unread_by(user)
20
+ find(:all) - find_read_by(user)
21
+ end
22
+
23
+ def find_read_by(user)
24
+ find(:all, :conditions => ["readings.readable_id = #{table_name}.id AND readings.user_id=?", user.id], :include => :readings)
25
+ end
26
+ end
27
+
28
+ module InstanceMethods
29
+ def read_by!(user)
30
+ readings << Reading.new(:user_id => user.id)
31
+ end
32
+
33
+ def unread_by!(user)
34
+ readings.find(:first, :conditions => ["user_id = ?",user.id])
35
+ end
36
+
37
+ def read_by?(user)
38
+ !!users_who_read.find(:first, :conditions => ["user_id = ?",user.id])
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
data/lib/reading.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Reading < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :readable, :polymorphic => true
4
+
5
+ validates_presence_of :user_id, :readable_id, :readable_type
6
+ validates_uniqueness_of :user_id, :scope => [:readable_id, :readable_type]
7
+ end
@@ -0,0 +1,3 @@
1
+ User.class_eval do
2
+ has_many :readings
3
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'acts-as-readable'
2
+ require 'reading'
3
+ require 'user_with_readings'
4
+
5
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::Readable
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcnetdev-acts-as-readable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.200807042
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-04 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "2.1"
23
+ version:
24
+ description: Wrapper around render :partial that removes the need to use :locals, and allows blocks to be taken easily
25
+ email: michael@intridea.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ files:
33
+ - README
34
+ - acts-as-readable.gemspec
35
+ - generators/acts_as_readable_migration/acts_as_readable_migration_generator.rb
36
+ - generators/acts_as_readable_migration/templates/migration.rb
37
+ - init.rb
38
+ - lib/acts-as-readable.rb
39
+ - lib/reading.rb
40
+ - lib/user_with_readings.rb
41
+ - rails/init.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/mbleigh/acts-as-readable
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --main
47
+ - README
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.2.0
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Makes calling partials in views look better and more fun.
69
+ test_files: []
70
+