Fold legacy has_view_count plugin into lib

This commit is contained in:
Luke Barratt 2015-08-23 15:43:40 +01:00
parent c1466c262a
commit 7738c2b845
11 changed files with 34 additions and 40 deletions

View file

@ -15,8 +15,6 @@
# text_coding :integer default(0), not null
#
require File.join(Rails.root, 'vendor', 'plugins', 'has_view_count', 'init.rb')
class Article < ActiveRecord::Base
include Exceptions
include Extra

View file

@ -0,0 +1 @@
require 'has_view_count'

View file

@ -0,0 +1,33 @@
module Citrus
module HasViewCount
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def has_view_count
has_many :view_counts, :as => :viewable, :dependent => :destroy
include Citrus::HasViewCount::InstanceMethods
end
end
module InstanceMethods
def record_view_count(ip_address, logged_in = false)
self.view_counts.create(:viewable => self, :ip_address => ip_address, :logged_in => logged_in)
return self
end
def view_count
self.view_counts.length
end
def view_count_string(str = "view")
return "#{view_count} #{str.singularize}" if view_count == 1
return "#{view_count} #{str.pluralize}" unless view_count == 1
end
end
end
end
ActiveRecord::Base.send(:include, Citrus::HasViewCount)

View file

@ -1 +0,0 @@
require File.join(Rails.root, 'vendor', 'plugins', 'has_view_count', 'lib', 'has_view_count.rb')

View file

@ -1,37 +0,0 @@
module Citrus
module HasViewCount
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def has_view_count
has_many :view_counts, :as => :viewable, :dependent => :destroy
include Citrus::HasViewCount::InstanceMethods
end
end
module InstanceMethods
def record_view_count(ip_address, logged_in = false)
self.view_counts.create(:viewable => self, :ip_address => ip_address, :logged_in => logged_in)
return self
end
def view_count
self.view_counts.length
end
def view_count_string(str = "view")
return "#{view_count} #{str.singularize}" if view_count == 1
return "#{view_count} #{str.pluralize}" unless view_count == 1
end
end
end
end
ActiveRecord::Base.send(:include, Citrus::HasViewCount)