Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Created April 30, 2014 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmcadam/11440146 to your computer and use it in GitHub Desktop.
Save aaronmcadam/11440146 to your computer and use it in GitHub Desktop.
class QuestionTagsManager < Draper::Decorator
include Draper::LazyHelpers
extend Forwardable
def_delegator :project, :id, :project_id
def_delegator :project, :title, :project_title
def_delegator :task, :id, :task_id
def_delegator :task, :type, :task_type
def_delegator :task, :title, :task_title
def_delegator :task, :description, :task_description
def_delegator :new_tag, :save, :save_tag
def_delegator :tag, :destroy_or_merge, :destroy_tag
def_delegator :tag, :update_attributes, :update_tag
def_delegator :question, :id, :question_id
def_delegator :question, :alias, :question_alias
def_delegator :question, :summary, :question_summary
def_delegator :policy, :update?, :can_update?
def_delegator :policy, :destroy?, :can_destroy?
def_delegator :policy, :create_tags?, :can_create_tags?
def initialize(params)
@params = params
end
def render_management_header
return unless can_create_tags?
render("question_tags/management_header")
end
def render_management_links_for(tag)
return unless can_create_tags?
render("question_tags/management_links", question: question, tag: tag)
end
def listing
tags.any? ? QuestionTagListing.new : NullQuestionTagListing.new
end
def tags
@_tags ||= question.tags.map { |tag| QuestionTag.new(tag.to_h) }
end
def tag
@_tag ||= find_tag
end
def new_tag
attributes = @params.fetch(:tag, {}).merge(question: question)
@_new_tag ||= QuestionTag.new(attributes)
end
def question
@_question ||= find_question
end
def task_icon
task.icon_tag(project_id)
end
private
def policy
@_policy ||= QuestionPolicy.new(question)
end
def find_tag
QuestionTag.find(@params[:id])
end
def find_question
Question.find_with_tags(@params[:question_id])
end
def task
@_task ||= TaskDecorator.decorate(Task.find(question.task.id))
end
def project
@_project ||= task_list.project
end
def task_list
@_task_list ||= Crowdlab::Model::TaskList.find(task.task_list.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment