# How to add plugin to ActiveRecord

**URL:** <https://www.ruby-forum.com/t/how-to-add-plugin-to-activerecord/62844>\
**Category:** Rails\
**Created:** [June 8, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/how-to-add-plugin-to-activerecord/62844 "2006-06-08T18:17:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [June 8, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/how-to-add-plugin-to-activerecord/62844/1 "2006-06-08T18:17:31Z")

</div>

I’m trying to set a date field’s day to the last day of the month before  
I save it. I can do this successfully now using this code:

class Account \< ActiveRecord::Base  
def before\_save  
self.exp\_date = last\_day\_of\_month(self.exp\_date)  
end

# Returns a Date object set to the last day of the current month

def last\_day\_of\_month(date = Date.today)  
next\_month\_str = date.year.to\_s + ‘-’ + (date.month + 1).to\_s +  
‘-01’  
Date.parse(next\_month\_str) - 1  
end  
end

However, this is Ruby on Rails, and I’m trying to break all my old  
programming habits like making ugly code, and this looks ugly. Is there  
a way to make this into some kind of plugin so that all I have to do is  
say:

class Account \< ActiveRecord::Base  
acts\_as\_expiration\_date :exp\_date  
end

and it will automatically do what I want behind the scenes?

Thanks!  
Mike

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [June 8, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/how-to-add-plugin-to-activerecord/62844/2 "2006-06-08T18:33:50Z")

</div>

Mike Haggerty wrote:

> class Account \< ActiveRecord::Base  
> acts\_as\_expiration\_date :exp\_date  
> end

Try looking at other acts\_as\_\* plugins and see how they work.

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [June 8, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/how-to-add-plugin-to-activerecord/62844/3 "2006-06-08T20:36:37Z")

</div>

if you are onyl using this for account models, a plugin is going to be  
overkill…why not just do:

class Account \< ActiveRecord::Base  
before\_create :set\_expiry\_date

# other methods

private

# set the expiry date

def set\_expiry\_date  
self.exp\_date = Time.now.end\_of\_month.to\_date.to\_s  
end  
end

ex:

> > Time.now.end\_of\_month.to\_date.to\_s  
> > =\> “2006-06-30”

note: if you use before\_save, every time you save the record, whether  
it’s a  
new record or an update, the expiry date will be updated to reflect the  
last  
day of the current month…in my example, I used before\_create to  
indicate i  
only want the expiry date set when a new record is created.

Chris
