Skip to content

Instantly share code, notes, and snippets.

@vajradog
Last active April 5, 2021 22:33
Show Gist options
  • Save vajradog/5487268ae2e2f6d3f3a8 to your computer and use it in GitHub Desktop.
Save vajradog/5487268ae2e2f6d3f3a8 to your computer and use it in GitHub Desktop.
Set auto expiration for rails models

##Set auto expiration for rails models.

This is a simple view logic that moves objects based on a predetermined date.

Let's say we have a bunch of events(or posts) on our index page and we want to sort out which of these are still active and which are not (based on the time).

We want the active ones to be clickable and prominent, we want the non-active ones to be greyed-out or even hidden. So how do we do this?

Rails provide a simple yet powerful method: future?()

First we must add an extra column for the expiration date, so there's something to compare.

rails g migration add_expire_to_posts

then in the migration XXXX_add_expire_to_posts.rb

class AddExpireToPosts < ActiveRecord::Migration
  def change
  	add_column :posts, :expire, :datetime
  end
end

rake db:migrate

permit the expire column in your posts controller

...
private

def post_params
  params.require(:post).permit(:title, :body, :expire)
end

now set that in your form aswell

...
<%= f.label :expire %><br>
<%= f.datetime_select :expire %>
...

We are pretty much done now, just change the view logics

For posts that are active and has not expired yet

<% @posts.each do |post| %>
<% if post.expire.future? %>
<%= post.title %> <%= post.body %>
<% end %>
<% end %>
For posts that have expired

<% @posts.each do |post| %>
<% if !post.expire.future? %>
<%= post.title %> <%= post.body %>
<% end %>
<% end %>

That's it. Using this information you can style, hide or do what you like with the two kinds of posts.

Using a jQuery calendar for the datetime field.

Here's a railscast tutorial on the same subject, I highly recommend subscribing to railscast: http://railscasts.com/episodes/213-calendars-revised

Here's what do you. In your gem file add

gem 'jquery-ui-rails'

then in your terminal

bundle

Then add the datepicker in your app/assets/javascripts/application.js

Here's an example of how the datepicker works: http://jqueryui.com/datepicker/

...
//= require jquery.ui.datepicker
...

also add it in the app/assets/stylesheets/application.css

...
*= require jquery.ui.datepicker
...

We will now need to change our form, from datetime_select to text_field, like so:

<%= f.label :expire %><br>
<%= f.datetime_select :expire %>

change that to this:

<%= f.label :expire %><br>
<%= f.text_field :expire %>

now we add the javascript functionality in our app/assets/javascripts/posts.js.coffee

jQuery ->
  $('#post_expire').datepicker
    dateFormat: 'yy-mm-dd'

now reload the page or just restart the server and go to the form page. The expire field on the form should be a text box but when you click on it, the calendar should appear.

In my case it did not so I had to disable turbolink, there are couple of ways to do that:

Option 1: disable turbolink for a particular link

<%= link_to 'New Post', new_post_path, :data => { :no_turbolink => true } %>

Option 2: disable turbolink entirely. In app/javascripts/application.js

//= require turbolinks

Change that to this:

// require turbolinks

Try restarting your server and test again.

Hope this helps you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment