Created
January 17, 2014 13:02
-
-
Save shosanna/8473021 to your computer and use it in GitHub Desktop.
Ukazka Journeyapp.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task < ActiveRecord::Base | |
attr_accessible :title, :description, :tag_ids | |
validates_presence_of :title, :description | |
has_many :solutions, dependent: :destroy | |
has_many :users, through: :solutions | |
has_many :taggings | |
has_many :tags, through: :taggings | |
belongs_to :next_step, class_name: "Task" | |
has_one :previous_step, class_name: "Task", foreign_key: "next_step_id" | |
# nasledujici dve metody se pouzivaji pouze pro zjisteni, zda u instance ukolu na kteoru je metoda solved_by volana | |
# ma uzivatel splnene solution - jinymi slovy, jestli ukol uz vyresil. | |
def solution_for(user) | |
solutions.where(user_id: user.id).first | |
end | |
def solved_by?(user) | |
!!solution_for(user) | |
end | |
# vraci pouze mozne ukoly pro uzivatele (ne ty, ktere jiz ma vyresene) | |
# nebo ty u kterych jeste nema vyreseny predchazejici krok - task.previous_step | |
def self.available_for(user) | |
Task.all.reject do |task| | |
task.solved_by?(user) || (task.previous_step && !task.previous_step.solved_by?(user)) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TasksController < AdminController | |
def index | |
@tasks = Task.all | |
end | |
def show | |
@task = Task.find(params[:id]) | |
end | |
def new | |
@task = Task.new | |
end | |
def create | |
@task = Task.new(params[:task]) | |
if @task.save | |
redirect_to tasks_path, notice: "Task was added." | |
else | |
render :new | |
end | |
end | |
def edit | |
@task = Task.find(params[:id]) | |
end | |
def update | |
@task = Task.find(params[:id]) | |
@task.update_attributes(params[:task]) | |
@task.save! | |
redirect_to tasks_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment