Our first assignment as new interns was to make Blog Posts that were published visible to everyone at the website, and the Blog Posts that were not published visible only to logged in users. After some research we figured out that we will do that by setting up scopes at the Blog Posts model and then change the index method in blog controller by assigning new variables.

class BlogPost < ActiveRecord::Base

  scope :published, -> { where(published: true) }
  scope :by_published, lambda{ |published| where(published: published == "true") }
  belongs_to :topic

  delegate :name, to: :topic,  allow_nil: true, prefix: true
  def index
    posts = BlogPost.all
    if current_user
      posts = posts
    else
      posts = BlogPost.published
    end
    render :index, locals: {posts: posts}
  end

Then we had to test it using both rspec and capybara gems. Couple of problems occured along the way, but with knowledge from reading gem documentation and with help from more experienced colleagues we were able to make tests pass.

  it "signs me in" do
    visit '/admin'
    within("#new_user") do
      fill_in 'Email', :with => 'user@example.com'
      fill_in 'Password', :with => 'qwertqwert' 
    end
    click_button 'Log in'
    expect(page).to have_content 'Witaj ponownie!'
    visit '/blog'
    expect(page).to have_content("blab")
  end

  it "checks if not logged sees unpublished" do
    visit '/blog'
    expect(BlogPost.all.count).to eq 5
    expect(page).to_not have_content("blab")
  end

    Next task we got was to create new feature - topics. The idea was to separate blog posts and assing each of them to corresponding topics either while making new posts or by editing those that were written before. To make this happen we had to make a new controller, model and view (based on blog posts) for admin mode. Then we had to generate migrations for new topics table, and also for adding a new column to blog posts (to see the connection between those two). First, and the biggest problem was caused by naming all new content "Threads" which turns out to be reserved by Rails. Because of that, we saw all sorts of errors, so when we figured out that it was the source  the problem we had to start from the scratch, and name all files "Topics". In order to make everything work the way we wanted, the most important thing was to connect blog post and topic models by belongs_to and has_many.

    Finally, topics tab was completed, logged in user could see how many and which posts are included in seperate topics. Another assignment was to enable filtering blogposts by topics, and by "published" value. We decided to do achieve that using simple form gem:

  = simple_form_for :filter, url: admin_blog_posts_path, html: { class: "form-horizontal"}, method: "get" do |f|
    =f.collection_radio_buttons :published, [[true, 'Opublikowane'] ,[false, 'Nieopublikowane']], :first, :last, checked: published
    = f.input :topic_id, :collection => topics, include_blank: true, required: false, selected: topic
    %br
    =f.submit

    By using this form, we choose topic from collection of topics (or we select empty space which stands for all of the topics and it's indicated by allow_blank), and choose radio button for "published" or not. By clicking submit we send params to the controller, where results are being stored by corresponding variables, which brings us to the final version of index action.

  def index
    blog_posts = BlogPost.order('created_at DESC')
    if params[:filter].present?
      topic = params[:filter][:topic_id]
      published = params[:filter][:published]
      blog_posts = blog_posts.by_published(published)  if published.present?
      blog_posts = blog_posts.where(topic_id: topic) if topic.present?
    end
    render :index, locals: { records: blog_posts, topics: Topic.all, topic: topic, published: published }
  end

   Finally, I would like to share couple of free sources that I'm learning rails from, for all of You that are just starting out, like I did 2 weeks ago :)

Getting started with Rails

Rails tutorial

Code Academy