I’m working on a system for managing multiple different items, let’s say
articles and pictures. All these items can be published and unpublished.
They have a published_at datetime field that is nil in unpublished
state, and filled out in published state.
This is the code that I use in the models:
class SomeModel < ActiveRecord::Base
attr_protected :published_at
def published?
!published_at.nil?
end
def pending?
!published? || Time.now < published_at
end
def status
pending? ? :pending : :published
end
def publish
self.published_at = Time.now if self.pending?
end
def unpublish
self.published_at = nil
end
end
But, I want to keep it DRY, and I do not want to add these lines to any
model that should be publishable. I tried to create a
publishable_item.rb model class and to inherit from that, but in this
case Rails will look for a publishable_items table.
But, I want to keep it DRY, and I do not want to add these lines to any
model that should be publishable. I tried to create a
publishable_item.rb model class and to inherit from that, but in this
case Rails will look for a publishable_items table.
an die Klasse anhängen, die diese Erweiterung aufruft
def self.included(klass)
klass.extend ClassMethods
end
module ClassMethods
def acts_as_publishable
include PublishableSystem::InstanceMethods
attr_protected :published_at
end
end
module InstanceMethods
def published?
!published_at.nil?
end
def pending?
!published? || Time.now < published_at
end
def status
pending? ? :pending : :published
end
def publish
self.published_at = Time.now if self.pending?
end
def unpublish
self.published_at = nil
end
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.