Ruby on Rails Friday, February 6, 2015



On Friday, February 6, 2015 at 11:33:03 AM UTC, Ruby-Forum.com User wrote:
My app/controllers/blog_controller.rb:


To be honest, I don't think many people (certainly not me) want to read though a wall of code to work out which bits are relevant and which aren't. It's up to you as question asker to do extract from your application what is relevant and what isn't. It would however be a lot clearer if you split out some of this into helpers rather than having it all inline in your view


Fred
 
class BlogController < ApplicationController
  require 'date'

  caches_page :index, :show

  def index
          if (params[:population_categories])
      @blog_posts = BlogPost.live.where(:population_category_ids =>
params[:population_categories]).latest.page(params[:page])
      # @blog_posts = BlogPost.find(:all, :conditions => ['created_at >
? AND created_at < ?', b_dt.strftime("%Y-%m-%d %H:%M"),
e_dt.strftime("%Y-%m-%d %H:%M")]).latest.page(params[:page])
    elsif (params[:month])
      b_dt = DateTime.strptime("01/" + params[:month].to_s + "/" +
params[:year] + " 00:00", "%d/%m/%Y %H:%M")
      e_dt = DateTime.strptime("01/" + (params[:month].to_i+1).to_s +
"/" + params[:year] + " 00:00", "%d/%m/%Y %H:%M")

      @blog_posts = BlogPost.live.where(:created_at =>
b_dt.strftime("%Y-%m-%d %H:%M DESC")..e_dt.strftime("%Y-%m-%d %H:%M
DESC") ).latest.page(params[:page] )

  #@blog_posts = @posts.group_by { |t| t.publication_date
.beginning_of_month }
    else
      @blog_posts =
BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])
    end
 @posts_by_month = BlogPost.all.group_by { |post|
post.created_at.strftime("%m %Y") }
      end
  def show
    @blog_post = BlogPost.where(:url_fragment =>
params[:url_fragment]).first
    raise ActionController::RoutingError.new('Not Found') if
@blog_post.nil?
    @blog_posts =
BlogPost.live.keyword(params[:keyword]).latest.page(params[:page])
  @posts_by_month = BlogPost.all.group_by { |post|
post.created_at.strftime("%m %Y") }
  end
  def feed
    @posts_feed = BlogPost.where(:content_state => 'live')
    respond_to do |format|
      format.atom { render layout: false }
    end
  end
end

 app/controllers/month_posts_controler:

class MonthPostsController < ApplicationController
  def display
  @posts_by_month = BlogPost.all.group_by { |post|
post.created_at.strftime("%B") }
  #@posts_by_month = BlogPost.all.each { |post|
post.created_at.strftime("December") }

  end
  def show
  @posts = BlogPost.all.find(params[:id])
  @posts = Post.includes(:comments).order("created_at DESC").limit(5)
end
end


app/controllers/post_controller:
def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
      format.atom
    end
end

app/views/blog/index.html.erb
<div class='section white' id='blog_posts'>
  <div class='section_content'>
    <div class='section-left'>

      <% @blog_posts.limit(3).each do |post| %>
        <% @section_keywords += post.keywords_array %>
        <%= render :partial => '/blog/index/blog_post', :locals =>
{:atom => post} %>
      <% end %>

    <div class="right-col">
      <h2>ARCHIVES</h2>
      <div class='posts_by_month'>

        <ul>
        <% archive_string = "" %>
        <% date_value = "" %>
        <% @posts_by_month.each do |monthname, posts| %>

          <% date_value = (Date::MONTHNAMES[monthname[0..-6].to_i]) + "
" + (monthname[3..8]) %>
          <% archive_string = "<li><a class='filter' href='/blog?month="
+ monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value +
"</a></li>" + archive_string %>
        <% end %>
        <%= raw archive_string %>
        </ul>


      </div>
    </div>
    <div class="right-col">
      <div class="blogpost-rss">
        <a class="rss-link" href="/feed.atom"></a>
        <%= auto_discovery_link_tag(:rss, feed_url(:format => :atom)) %>

      </div>
    </div>

app/views/blog/show.html.erb:
<div class='section white blog_post blog_detail' id='blog_post'>

  <div class='section_content'>
         <div class="right-col">
    <h2>ARCHIVES</h2>
    <div class='posts_by_month'>
      <ul>
  <% archive_string = "" %>
  <% date_value = "" %>
  <% @posts_by_month.each do |monthname, posts| %>
  <% date_value = (Date::MONTHNAMES[monthname[0..-6].to_i]) + " " +
(monthname[3..8]) %>
  <% if(date_value == pub_date) %>
  <% archive_string = "<li class='active'><a class='filter'
href='/blog?month=" + monthname[0..-6] + "&year=" + monthname[3..8] +
"'>" + date_value + "</a></li>" + archive_string %>
        <% else %>
    <% archive_string = "<li><a class='filter' href='/blog?month=" +
monthname[0..-6] + "&year=" + monthname[3..8] + "'>" + date_value +
"</a></li>" + archive_string %>
        <% end %>
        <% end %>
      <%= raw archive_string %>
      </ul>

      </div>
      </div>

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/74f1424e-4cff-46f6-8fc6-fff5498d5171%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment