# Add functionality to multiple models?

**URL:** <https://www.ruby-forum.com/t/add-functionality-to-multiple-models/103505>\
**Category:** Rails\
**Created:** [May 7, 2007, 10:35am UTC](https://www.ruby-forum.com/t/add-functionality-to-multiple-models/103505 "2007-05-07T10:35:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [May 7, 2007, 10:35am UTC](https://www.ruby-forum.com/t/add-functionality-to-multiple-models/103505/1 "2007-05-07T10:35:02Z")

</div>

Hello,

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.

So I’m looking forward to your suggestions.

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [May 7, 2007, 11:55am UTC](https://www.ruby-forum.com/t/add-functionality-to-multiple-models/103505/2 "2007-05-07T11:55:53Z")

</div>

On May 7, 11:35 am, rv dh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> […]  
> 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.

Try using a mixin (module).

[http://www.rubycentral.com/book/tut\_modules.html](http://www.rubycentral.com/book/tut_modules.html)

---

<div class="post-metadata">

**Author:** ![ralph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f475e1/32.png) [@ralph](https://www.ruby-forum.com/u/ralph)\
**Post date:** [May 7, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/add-functionality-to-multiple-models/103505/3 "2007-05-07T16:42:02Z")

</div>

Trochalakis C. wrote:

> Try using a mixin (module).
> 
> [http://www.rubycentral.com/book/tut\_modules.html](http://www.rubycentral.com/book/tut_modules.html)

Thanks, that did it. For anyone interested, the mixin looks like this  
(model must have attribute published\_at):

In any model:  
include PublishableSystem  
acts\_as\_publishable

#lib/publishable\_system.rb:  
module PublishableSystem

# 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
