Michael @ Work :: Space

Active Admin defaults, DateTime stuff and Rails 4.2 new cool truncate_words() metod

Here is my today's progress:

  • Since yesterday's evening I was looking for a solution to a problem with setting the default value to the published_at field in my ActiveAdmin panel. And this morning I found it
form :html => { :enctype => "multipart/form-data" } do |f| 
  f.object.published_at = DateTime.now.change(offset: "-3000") 
  f.inputs "Post Details" do 
    f.input :title 
    f.input :body 
    f.input :published_at 
    ... 
  end 
  f.actions 
end
  • Also learned a simple yet not obvious to me way (meaning it's MVC methodology) to set the default value for the form in controller. Like this:
  def new 
    @post = Post.new(published_at: DateTime.now.change(offset: "-3000")) 
  end

For some reason these two doesn't work in my case:

DateTime.now.utc.localtime
DateTime.now.utc.in_time_zone("Moscow")
  • There is a new great method in Rails 4.2 which is called truncate_words(). I needed to truncate my posts on the main page at a certain position which I could set myself for every post. Previous Rails truncate method doesn't work for this purpose. But the new one works just fine:
  def truncate_post(post_body, sep = '_sep')
    post_body.truncate_words(1, {separator: sep})  
  end

So I can set the separation place myself by typing in . Which is pretty cool. But I need to remove this string from my post template and I did it this way:

  def remove_separator(post_body, sep = '_sep')
    post_body.gsub(sep, '')
  end

But now I'm starting to think that it's still not the best solution. And the best would be to have a separate field in Post for the index page. In which I may briefly describe what is this post about. I think I'm gonna try this way soon.

  • Installed Disqus to the blog (feel free to test it with me and say Hello :)
  • Added Friendly_id gem so now my URLs look nice