Skip to content

Instantly share code, notes, and snippets.

@beliar91
beliar91 / task_spec.rb
Created January 30, 2016 20:34
spec/models/task_spec.rb
it "Counting average execution time should return proper results" do
task_1 = Task.create(name: "ABCD", status: "Created", completion_time:30)
task_2 = Task.create(name: "ABCDE", status: "Created", completion_time: 70)
sum_of_completion_time = task_1.completion_time + task_2.completion_time
number_of_tasks = Task.all.count
average_execution_time = sum_of_completion_time / number_of_tasks
expect(average_execution_time).to eq(task_2.count_average_execution_time)
@beliar91
beliar91 / Task.rb
Created January 28, 2016 11:30
spec/models/task.rb
require 'rails_helper'
RSpec.describe Task, type: :model do
let(:task) {FactoryGirl.create(:task)}
let(:task_created_1) {FactoryGirl.create(:task_created_1)}
let(:task_created_2) {FactoryGirl.create(:task_created_2)}
let(:task_in_progress_1) {FactoryGirl.create(:task_in_progress_1)}
let(:task_in_progress_2) {FactoryGirl.create(:task_in_progress_2)}
@beliar91
beliar91 / Tasks.rb
Created January 28, 2016 11:28
spec/factories/tasks.rb
FactoryGirl.define do
factory :task do |t|
t.status "Created"
t.completion_time 10
factory :task_created_1 do |t|
t.status "Created"
t.completion_time 90
end
@beliar91
beliar91 / Task.rb
Created January 28, 2016 11:25
models/Task
class Task < ActiveRecord::Base
validates :status, inclusion: {in: ["Created", "In Progress", "Completed"]}
scope :by_status, -> (status) {where(status: status)}
after_create :count_average_execution_time
belongs_to :house_hold
@beliar91
beliar91 / task_spec.rb
Created January 27, 2016 02:41
spec/models/task_spec.rb
require 'rails_helper'
RSpec.describe Task, type: :model do
let(:task2) {FactoryGirl.create(:task2)}
let(:task) {FactoryGirl.create(:task)}
let(:task_not_valid) {FactoryGirl.create(:task_not_valid, status: "Not valid")}
it "Task with not valid status should not be saved" do
@beliar91
beliar91 / task.rb
Created January 27, 2016 02:39
spec/factories/task.rb
FactoryGirl.define do
factory :task do |t|
t.status "Created"
t.completion_time 10
end
factory :task_two do |t|
t.status "Created"
t.completion_time 20
end
@beliar91
beliar91 / _index.html.erb
Created December 21, 2015 10:43
comments/_index.html.erb
<h2>Comments</h2>
<% comments.each do |comment| %>
<p><%= comment.content %> <%= link_to "Usuń", [@commentable, comment], method: :delete %></p>
<% end %>
class CommentsController < ApplicationController
before_action :load_commentable, only: [:create, :index, :destroy]
before_action :find_comment, only: [:destroy]
def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
redirect_to @commentable
end
end