acts_as_important 0.1.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: a15ea261b84c9494ce983efa27865b1af4123640
4
+ data.tar.gz: e43c33d759fc7698577a58d27096790b7ddf5b2e
5
+ SHA512:
6
+ metadata.gz: 16b6c8164c8eb716e28d1c84855ef909b95825f26ef56b02e102e6edd1300ba3a3769d066fa3e2820ff3538d4435ce99e9551f729e0d358cd06a089f74b005b3
7
+ data.tar.gz: c437495f64b0432e384d14d479d0c829db32a09104e3dede3e1d6d79505b94ef3bd472f82572520197ae54a8588b0d610b9fecbdb14c1b150ea4d0034e345f5a
@@ -0,0 +1,84 @@
1
+ module ActsAsImportant
2
+ module ActMethod #:nodoc:
3
+ def acts_as_important(options = {})
4
+ has_many :importance_indicators, :as => :record, :dependent => :destroy
5
+
6
+ has_many :active_importance_indicators, :class_name => 'ImportanceIndicator', :as => :record, :dependent => :destroy, :conditions =>['active = ?', true]
7
+ has_many :concerned_users, :through => :active_importance_indicators, :source => :user
8
+
9
+ # Left joins importance indicators from a particular user
10
+ scope :with_user_importance, lambda{|user| joins("LEFT OUTER JOIN importance_indicators ON importance_indicators.record_id = #{quoted_table_name}.id AND importance_indicators.record_type = '#{name}' AND importance_indicators.user_id = #{user.id}")}
11
+
12
+ scope :important_to, lambda{|user| joins(:importance_indicators).where("importance_indicators.user_id = #{user.id} AND importance_indicators.active = true")}
13
+ scope :was_important_to, lambda{|user| joins(:importance_indicators).where("importance_indicators.user_id = #{user.id} AND importance_indicators.active = false")}
14
+ scope :not_important_to, lambda{|user| with_user_importance(user).where("importance_indicators.record_id IS NULL")}
15
+ scope :important_to_user_first, lambda{|user| with_user_importance(user).reorder('importance_indicators.record_id IS NOT NULL DESC')}
16
+
17
+ extend ActsAsImportant::ClassMethods
18
+ include ActsAsImportant::InstanceMethods
19
+ end
20
+
21
+ # Call this method from the user model
22
+ def concerned_with_importance
23
+ has_many :importance_indicators
24
+ end
25
+ end
26
+
27
+ module ClassMethods
28
+ # Find all the importance_indicators of the records by the user in a single SQL query and cache them in the records for use in the view.
29
+ def cache_importance_for(records, user)
30
+ importance_indicators = []
31
+ ImportanceIndicator.where(:record_type => name, :record_id => records.collect(&:id), :user_id => user.id).each do |importance_indicator|
32
+ importance_indicators[importance_indicator.record_id] = importance_indicator
33
+ end
34
+
35
+ for record in records
36
+ record.cached_importance = importance_indicators[record.id] || false
37
+ end
38
+
39
+ return importance_indicators
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ attr_accessor :cached_importance
45
+
46
+ def acts_like_important?
47
+ true
48
+ end
49
+
50
+ def important_to!(user)
51
+ importance_indicators.create(:user_id => user.id) unless important_to?(user)
52
+ rescue ActiveRecord::RecordNotUnique # Database-level uniqueness constraint failed.
53
+ return true
54
+ end
55
+
56
+ def important_to?(user)
57
+ if indicator = importance_indicator_for(user)
58
+ indicator.active?
59
+ end
60
+ end
61
+
62
+ def was_important_to?(user)
63
+ if indicator = importance_indicator_for(user)
64
+ !indicator.active?
65
+ end
66
+ end
67
+
68
+ def importance_indicator_for(user)
69
+ case cached_importance
70
+ when nil
71
+ importance_indicators.loaded? ? importance_indicators.detect{|i| i.user_id == user.id} : importance_indicators.find_by_user_id(user.id)
72
+ when false
73
+ nil
74
+ else
75
+ cached_importance
76
+ end
77
+ end
78
+
79
+ def importance_note_for(user)
80
+ importance_indicator_for(user).try(:note)
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,4 @@
1
+ class ImportanceIndicator < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :record, :polymorphic => true
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'acts_as_important/acts_as_important'
2
+ require 'acts_as_important/importance_indicator'
3
+
4
+ ActiveRecord::Base.extend ActsAsImportant::ActMethod
@@ -0,0 +1,17 @@
1
+ class ActsAsImportantMigrationGenerator < Rails::Generators::Base
2
+ def create_migration_file
3
+ create_file "db/migrations/initializer.rb", <<-EOV
4
+ class ActsAsImportantTables < ActiveRecord::Migration
5
+ def change
6
+ create_table :importance_indicators do |t|
7
+ t.belongs_to :user
8
+ t.belongs_to :record, :polymorphic => true
9
+ t.text :note
10
+ end
11
+
12
+ add_index :importance_indicators, [:record_id, :record_type], :name => 'index_importance_indicators', :unique => true
13
+ end
14
+ end
15
+ EOV
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_important
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Jakobsen
8
+ - Ryan Wallace
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.1'
28
+ description:
29
+ email: contact@culturecode.ca
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/acts_as_important.rb
35
+ - lib/acts_as_important/acts_as_important.rb
36
+ - lib/acts_as_important/importance_indicator.rb
37
+ - lib/generators/migration_generator.rb
38
+ homepage: http://github.com/culturecode/acts_as_important
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.1
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Allows the you track what records are important to users and why.
61
+ test_files: []