Module: SanteyView::Viewable::ClassMethods
- Defined in:
- lib/santey_view/viewable.rb
Instance Method Summary collapse
-
#add_viewings_columns ⇒ Object
Create the needed columns for santey_view.
-
#find_viewed_by(viewer) ⇒ Object
Find all viewings for a specific viewer.
- #generate_viewings_columns(table) ⇒ Object
-
#remove_viewings_columns ⇒ Object
Remove the santey_view specific columns added with add_viewings_columns To be used during migration, but can also be used in other places.
- #santey_view(options = {}) ⇒ Object
Instance Method Details
#add_viewings_columns ⇒ Object
Create the needed columns for santey_view. To be used during migration, but can also be used in other places.
65 66 67 68 69 70 |
# File 'lib/santey_view/viewable.rb', line 65 def add_viewings_columns if !self.content_columns.find { |c| 'views' == c.name } self.connection.add_column table_name, :views, :integer, :default => '0' self.reset_column_information end end |
#find_viewed_by(viewer) ⇒ Object
Find all viewings for a specific viewer.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/santey_view/viewable.rb', line 82 def find_viewed_by viewer viewing_class = [:viewing_class].constantize if !([:viewer_class].constantize === viewer) raise ViewedError, "The viewer object must be the one used when defining santey_view (or a descendent of it). other objects are not acceptable" end raise ViewedError, "Viewer must be a valid and existing object" if viewer.nil? || viewer.id.nil? raise ViewedError, 'Viewer must be a valid viewer' if !viewing_class.column_names.include? "viewer_id" viewed_class = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s conds = [ 'viewed_type = ? AND viewer_id = ?', viewed_class, viewer.id ] [:viewing_class].constantize.find(:all, :conditions => conds).collect {|r| r.viewed_type.constantize.find_by_id r.viewed.id } end |
#generate_viewings_columns(table) ⇒ Object
59 60 61 |
# File 'lib/santey_view/viewable.rb', line 59 def generate_viewings_columns table table.column :views, :integer end |
#remove_viewings_columns ⇒ Object
Remove the santey_view specific columns added with add_viewings_columns To be used during migration, but can also be used in other places
74 75 76 77 78 79 |
# File 'lib/santey_view/viewable.rb', line 74 def remove_viewings_columns if self.content_columns.find { |c| 'views' == c.name } self.connection.remove_column table_name, :views self.reset_column_information end end |
#santey_view(options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/santey_view/viewable.rb', line 10 def santey_view( = {}) # don't allow multiple calls return if self.included_modules.include?(SanteyView::Viewable::ViewMethods) include SanteyView::Viewable::ViewMethods viewing_class = [:viewing_class] || 'Viewing' viewer_class = [:viewer_class] || 'User' unless Object.const_defined?(viewing_class) Object.class_eval <<-EOV class #{viewing_class} < ActiveRecord::Base belongs_to :viewed, :polymorphic => true end EOV if Object.const_defined?(viewer_class) Object.class_eval <<-EOV class #{viewing_class} < ActiveRecord::Base belongs_to :viewer, :class_name => #{viewer_class}, :foreign_key => :viewer_id end EOV end end write_inheritable_attribute( :santey_view_options , { :viewing_class => viewing_class, :viewer_class => viewer_class } ) class_inheritable_reader :santey_view_options class_eval do has_many :viewings, :as => :viewed, :dependent => :delete_all, :class_name => viewing_class.to_s if Object.const_defined?(viewer_class) has_many(:viewers, :through => :viewings, :class_name => viewer_class.to_s) end before_create :init_viewing_fields end # Add to the Viewer a has_many viewings if Object.const_defined?(viewer_class) viewer_as_class = viewer_class.constantize return if viewer_as_class.instance_methods.include?('find_in_viewings') viewer_as_class.class_eval <<-EOS has_many :viewings, :foreign_key => :viewer_id, :class_name => #{viewing_class.to_s} EOS end end |