2013-04-29 15:15:48 +02:00
|
|
|
module Todos
|
|
|
|
|
class TodoCreateParamsHelper
|
|
|
|
|
|
2013-04-30 23:02:54 -05:00
|
|
|
attr_reader :new_project_created, :new_context_created
|
2013-04-29 15:15:48 +02:00
|
|
|
|
|
|
|
|
def initialize(params, user)
|
|
|
|
|
@params = params['request'] || params
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes = params['request'] && params['request']['todo'] || params['todo']
|
|
|
|
|
@attributes = {} if @attributes.nil? # make sure there is at least an empty hash
|
2013-04-29 15:15:48 +02:00
|
|
|
@user = user
|
|
|
|
|
@errors = []
|
|
|
|
|
|
2013-04-30 23:02:54 -05:00
|
|
|
if @attributes[:tags]
|
2013-04-29 15:15:48 +02:00
|
|
|
# for single tags, @attributed[:tags] returns a hash. For multiple tags,
|
|
|
|
|
# it with return an array of hashes. Make sure it is always an array of hashes
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes[:tags][:tag] = [@attributes[:tags][:tag]] unless @attributes[:tags][:tag].class == Array
|
2013-04-29 15:15:48 +02:00
|
|
|
# the REST api may use <tags> which will collide with tags association, so rename tags to add_tags
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes[:add_tags] = @attributes[:tags]
|
|
|
|
|
@attributes.delete :tags
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@new_project_created = find_or_create_group(:project, user.projects, project_name)
|
|
|
|
|
@new_context_created = find_or_create_group(:context, user.contexts, context_name)
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes["starred"] = (params[:new_todo_starred]||"").include? "true" if params[:new_todo_starred]
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
2013-04-30 23:02:54 -05:00
|
|
|
def attributes
|
|
|
|
|
@attributes
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show_from
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes['show_from']
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def due
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes['due']
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def project_name
|
2013-04-30 23:01:46 -05:00
|
|
|
@params['project_name'].strip unless @params['project_name'].nil?
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def project_id
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes['project_id']
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def context_name
|
2013-04-30 23:01:46 -05:00
|
|
|
@params['context_name'].strip unless @params['context_name'].nil?
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def context_id
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes['context_id']
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tag_list
|
|
|
|
|
@params['tag_list']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def predecessor_list
|
|
|
|
|
@params['predecessor_list']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse_dates()
|
|
|
|
|
@attributes['show_from'] = @user.prefs.parse_date(show_from)
|
|
|
|
|
@attributes['due'] = @user.prefs.parse_date(due)
|
|
|
|
|
@attributes['due'] ||= ''
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sequential?
|
|
|
|
|
return !@params[:todos_sequential].blank? && @params[:todos_sequential]=='true'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def specified_by_name?(group_type)
|
|
|
|
|
return send("#{group_type}_specified_by_name?")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def specified_by_id?(group_type)
|
|
|
|
|
group_id = send("#{group_type}_id")
|
2013-04-29 20:38:35 -05:00
|
|
|
!group_id.blank?
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def project_specified_by_name?
|
2013-04-30 23:02:54 -05:00
|
|
|
return false unless @attributes['project_id'].blank?
|
2013-04-29 15:15:48 +02:00
|
|
|
return false if project_name.blank?
|
|
|
|
|
return false if project_name == 'None'
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def context_specified_by_name?
|
2013-04-30 23:02:54 -05:00
|
|
|
return false unless @attributes['context_id'].blank?
|
2013-04-29 15:15:48 +02:00
|
|
|
return false if context_name.blank?
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def add_errors(model)
|
2013-04-29 20:38:35 -05:00
|
|
|
@errors.each {|e| model.errors[ e[:attribute] ] = e[:message] }
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def find_or_create_group(group_type, set, name)
|
|
|
|
|
return set_id_by_name(group_type, set, name) if specified_by_name?(group_type)
|
2013-04-30 23:02:54 -05:00
|
|
|
return set_id_by_id_string(group_type, set, @attributes["#{group_type}_id"]) if specified_by_id?(group_type)
|
2013-04-29 15:15:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_id_by_name(group_type, set, name)
|
|
|
|
|
group = set.where(:name => name).first_or_create
|
2013-04-30 23:02:54 -05:00
|
|
|
@attributes["#{group_type}_id"] = group.id
|
2013-04-29 15:15:48 +02:00
|
|
|
return group.new_record_before_save?
|
2013-04-29 20:38:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_id_by_id_string(group_type, set, id)
|
2013-04-30 23:02:54 -05:00
|
|
|
# be aware, this will replace the project_id/context_id (string) in @attributes with the new found id (int)
|
|
|
|
|
@attributes["#{group_type}_id"] = set.find(id).id
|
2013-04-29 20:38:35 -05:00
|
|
|
return false
|
|
|
|
|
rescue
|
|
|
|
|
@errors << { :attribute => group_type, :message => "unknown"}
|
|
|
|
|
end
|
2013-04-29 15:15:48 +02:00
|
|
|
|
|
|
|
|
end
|
2013-04-29 20:38:35 -05:00
|
|
|
end
|