Module: YeshouaCrm::ActsAsViewed::Viewed::ClassMethods

Defined in:
lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb,
lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb

Instance Method Summary collapse

Instance Method Details

#add_viewings_columnsObject

Create the needed columns for acts_as_viewed. To be used during migration, but can also be used in other places.



209
210
211
212
213
214
215
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 209

def add_viewings_columns
  if !self.content_columns.find { |c| 'views' == c.name }
    self.connection.add_column table_name, :views, :integer, :default => '0'
    self.connection.add_column table_name, :total_views, :integer, :default => '0'
    self.reset_column_information
  end
end

#create_viewings_table(options = {}) ⇒ Object

Create the viewings table

Options hash:

  • :table_name - use a table name other than viewings

To be used during migration, but can also be used in other places



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 231

def create_viewings_table(options = {})
  name = options[:table_name] || :viewings
  if !self.connection.table_exists?(name)
    self.connection.create_table(name) do |t|
      t.column :viewer_id,   :integer
      t.column :viewed_id,   :integer
      t.column :viewed_type, :string
      t.column :ip, :string, :limit => '24'
      t.column :created_at, :datetime
    end

    self.connection.add_index(name, :viewer_id)
    self.connection.add_index(name, [:viewed_type, :viewed_id])
  end
end

#drop_viewings_table(options = {}) ⇒ Object

Drop the viewings table.

Options hash:

  • :table_name - the name of the viewings table, defaults to viewings

To be used during migration, but can also be used in other places



251
252
253
254
255
256
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 251

def drop_viewings_table(options = {})
  name = options[:table_name] || :viewings
  if self.connection.table_exists?(name)
    self.connection.drop_table(name)
  end
end

#find_viewed_by(viewer) ⇒ Object

Find all viewings for a specific viewer.

Raises:



259
260
261
262
263
264
265
266
267
268
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 259

def find_viewed_by(viewer)
  viewing_class = acts_as_viewed_options[:viewing_class].constantize
  if !(acts_as_viewed_options[:viewer_class].constantize === viewer)
    raise ViewedError, "The viewer object must be the one used when defining acts_as_viewed (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')
  conds = ['viewed_type = ? AND viewer_id = ?', self.name, viewer.id]
  acts_as_viewed_options[:viewing_class].constantize.where(conds).collect { |r| r.viewed_type.constantize.find_by_id r.viewed.id }
end

#generate_viewings_columns(table) ⇒ Object

Generate the viewings columns on a table, to be used when creating the table in a migration. This is the preferred way to do in a migration that creates new tables as it will make it as part of the table creation, and not generate ALTER TABLE calls after the fact



202
203
204
205
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 202

def generate_viewings_columns(table)
  table.column :views, :integer # uniq views
  table.column :total_views, :integer
end

#rcrm_acts_as_viewed(options = {}) ⇒ Object

Make the model viewable. The Viewing model, holding the details of the viewings, will be created dynamically if it doesn’t exist.

  • Adds a has_many :viewings association to the model for easy retrieval of the detailed viewings.

  • Adds a has_many :viewers association to the object.

  • Adds a has_many :viewings associations to the viewer class.

Options

  • :viewing_class - class of the model used for the viewings. Defaults to Viewing. This class will be dynamically created if not already defined. If the class is predefined, it must have in it the following definitions: belongs_to :viewed, :polymorphic => true belongs_to :viewer, :class_name => 'User', :foreign_key => :viewer_id replace user with the viewer class if needed.

  • :viewer_class - class of the model that creates the viewing. Defaults to User This class will NOT be created, so it must be defined in the app. Use the IP address to prevent multiple viewings from the same client.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 78

def rcrm_acts_as_viewed(options = {})
  # don't allow multiple calls
  return if self.included_modules.include?(ActsAsViewed::Viewed::ViewMethods)
  send :include, ActsAsViewed::Viewed::ViewMethods

  # Create the model for ratings if it doesn't yet exist
  viewing_class = options[:viewing_class] || 'Viewing'
  viewer_class  = options[:viewer_class]  || 'User'

  unless Object.const_defined?(viewing_class)
    Object.class_eval <<-EOV
      class #{viewing_class} < ActiveRecord::Base
        belongs_to :viewed, :polymorphic => true
        belongs_to :viewer, :class_name => #{viewer_class}, :foreign_key => :viewer_id
      end
    EOV
  end

  # Rails < 3
  # write_inheritable_attribute( :acts_as_viewed_options ,
  #                                { :viewing_class => viewing_class,
  #                                  :viewer_class => viewer_class } )
  # class_inheritable_reader :acts_as_viewed_options

  # Rails >= 3
  class_attribute :acts_as_viewed_options
  self.acts_as_viewed_options = { :viewing_class => viewing_class,
                                  :viewer_class => viewer_class }
  class_eval do
    has_many :viewings, :as => :viewed, :dependent => :delete_all, :class_name => viewing_class.to_s
    has_many(:viewers, :through => :viewings, :class_name => viewer_class.to_s)

    before_create :init_viewing_fields
  end

  # Add to the User (or whatever the viewer is) a has_many viewings
  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

#remove_viewings_columnsObject

Remove the acts_as_viewed specific columns added with add_viewings_columns To be used during migration, but can also be used in other places



219
220
221
222
223
224
225
# File 'lib/yeshoua_crm/acts_as_viewed/rcrm_acts_as_viewed.rb', line 219

def remove_viewings_columns
  if self.content_columns.find { |c| 'views' == c.name }
    self.connection.remove_column table_name, :views
    self.connection.remove_column table_name, :total_views
    self.reset_column_information
  end
end