Module: Redmineup::ActsAsViewed::Viewed::ClassMethods

Defined in:
lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb,
lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb

Instance Method Summary collapse

Instance Method Details

#add_viewings_columns ⇒ Object

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



214
215
216
217
218
219
220
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 214

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


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 236

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 => '45'
      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


256
257
258
259
260
261
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 256

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:



264
265
266
267
268
269
270
271
272
273
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 264

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



207
208
209
210
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 207

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

#remove_viewings_columns ⇒ Object

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



224
225
226
227
228
229
230
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 224

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

#up_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
120
121
122
123
124
# File 'lib/redmineup/acts_as_viewed/up_acts_as_viewed.rb', line 78

def up_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')
  # Subclasses loaded earlier (Person < User) keep their own association set, so add it to them as well.
  ([viewer_as_class] + viewer_as_class.descendants).each do |klass|
    next if klass.reflect_on_association(:viewings)

    klass.class_eval do
      has_many :viewings, foreign_key: :viewer_id, class_name: viewing_class.to_s, dependent: :delete_all
    end
  end
end