Guest User

Untitled

a guest
Feb 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. #--
  2. # $Id: acts_as_subscribable.rb 1061 2006-12-01 16:57:39Z mark $
  3. #
  4. # Copyright (c) 2006 Mark Daggett
  5. #
  6. # This file is part of RAM (Ruby Asset Manager)
  7. #
  8. # Released under the MIT / X11 License. See LICENSE file for details.
  9. #++
  10.  
  11. module ActiveRecord
  12. module Acts #:nodoc:
  13. module Subscribable #:nodoc:
  14. def self.included(base)
  15. base.extend(ClassMethods)
  16. end
  17.  
  18. module ClassMethods
  19. def acts_as_subscribable(options = {:subscribe_to=>[]})
  20.  
  21. # The mixee is subscribed to other models
  22. is_a_subscriber unless options[:subscribe_to].empty?
  23.  
  24. # The mixee has other classes subscribing to it
  25. is_subscribed_to if options[:subscribe_to].empty?
  26.  
  27. include ActiveRecord::Acts::Subscribable::InstanceMethods
  28. extend ActiveRecord::Acts::Subscribable::SingletonMethods
  29. end
  30.  
  31. # For example a User is_a_subscriber to Feeds
  32. def is_a_subscriber
  33.  
  34. has_many :subscriptions, :foreign_key => 'subscriber_id', :conditions => 'subscriptions.subscriber_type = ' +"'#{self.class_name}'" do
  35. # Override the << method so that it doesn't fuk up the association proxy.
  36. def <<(subscription)
  37. return unless @owner.subscriptions.map{|s| return false if s.subscribed_to_id == subscription.id && s.subscribed_to_type == subscription.class.class_name}
  38. s = Subscription.create(
  39. :subscriber_id => @owner.id,
  40. :subscriber_type => @owner.class.class_name,
  41. :subscribed_to_id => subscription.id,
  42. :subscribed_to_type => subscription.class.class_name
  43. )
  44. @owner.subscriptions(true)
  45. end
  46.  
  47. # @reader.subscriptions.include?(@magazine) #=> true / false
  48. def include?(subscription)
  49. @owner.subscriptions.map{|s| return true if s.subscribed_to_id == subscription.id && s.subscribed_to_type == subscription.class.class_name}
  50. return false
  51. end
  52. end
  53.  
  54. # Dynamically create methods which map to the specific types of subscriptions
  55. module_eval do
  56. define_method("books") do
  57. puts "foo"
  58. end
  59. end
  60. breakpoint
  61. end
  62.  
  63. # For example a Feed is subscribed_to by A User.
  64. def is_subscribed_to
  65.  
  66. # Why I am mixing single and double quotes in the finder_sql statement you ask?
  67. # Excellent question! The answer is when you use "double quotes" the string is
  68. # interpolated immediately in current class. When you use 'single quotes' the
  69. # string is interpolated by Rails in the context of a class instance.
  70. # I use single quotes to get the correct id and double qoutes to get the correct
  71. # classname.
  72.  
  73. has_many :subscribers, :class_name => 'Subscription', :finder_sql =>
  74. 'SELECT subscriptions.* ' +
  75. 'FROM subscriptions ' +
  76. 'WHERE subscriptions.subscribed_to_id = #{self.id} AND subscriptions.subscribed_to_type =' + "'#{self.class_name}'" do
  77.  
  78. # Override the << method so that it doesn't fuk up the association proxy.
  79. def <<(subscriber)
  80. return unless @owner.subscribers.map{|s| return false if s.subscriber_id == subscriber.id && s.subscriber_type == subscriber.class.class_name}
  81. s = Subscription.create(
  82. :subscriber_id => subscriber.id,
  83. :subscriber_type => subscriber.class.class_name,
  84. :subscribed_to_id => @owner.id,
  85. :subscribed_to_type => @owner.class.class_name
  86. )
  87. @owner.subscribers(true)
  88. end
  89.  
  90. # @feed.subscribers.include?(@reader) #=> true / false
  91. def include?(subscriber)
  92. @owner.subscribers.map{|s| return true if s.subscriber_id == subscriber.id && s.subscriber_type == subscriber.class.class_name}
  93. return false
  94. end
  95.  
  96. # @feed.subscribers.unsubscribe << @feed.subscribers[0]
  97. def unsubscribe(remove_me)
  98. @owner.subscribers.map{|subscription| subscription.destroy if subscription.subscriber_id == remove_me.id}
  99. @owner.subscribers(true)
  100. end
  101.  
  102. # @feed.unsubscribe_all
  103. def unsubscribe_all
  104. @owner.subscribers.each{|subscription| subscription.destroy}
  105. @owner.subscribers(true)
  106. end
  107.  
  108. end
  109. end
  110. end
  111.  
  112. module SingletonMethods
  113.  
  114. end
  115.  
  116. module InstanceMethods
  117.  
  118. end
  119.  
  120. end
  121. end
  122. end
Add Comment
Please, Sign In to add comment