mirror of
https://github.com/TracksApp/tracks.git
synced 2026-01-04 08:18:50 +01:00
Use an attr_reader for attributes
This commit is contained in:
parent
1c2de3fd5a
commit
6376bd7ef6
1 changed files with 19 additions and 20 deletions
|
|
@ -1,39 +1,38 @@
|
|||
module Todos
|
||||
class TodoCreateParamsHelper
|
||||
|
||||
attr_reader :new_project_created, :new_context_created
|
||||
attr_reader :new_project_created, :new_context_created, :attributes
|
||||
|
||||
def initialize(params, user)
|
||||
@params = params['request'] || params
|
||||
@attributes = params['request'] && params['request']['todo'] || params['todo']
|
||||
@attributes = {} if @attributes.nil? # make sure there is at least an empty hash
|
||||
@attributes = find_attributes(params)
|
||||
@user = user
|
||||
@errors = []
|
||||
|
||||
if @attributes[:tags]
|
||||
if attributes[:tags]
|
||||
# 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
|
||||
@attributes[:tags][:tag] = [@attributes[:tags][:tag]] unless @attributes[:tags][:tag].class == Array
|
||||
attributes[:tags][:tag] = [attributes[:tags][:tag]] unless attributes[:tags][:tag].class == Array
|
||||
# the REST api may use <tags> which will collide with tags association, so rename tags to add_tags
|
||||
@attributes[:add_tags] = @attributes[:tags]
|
||||
@attributes.delete :tags
|
||||
attributes[:add_tags] = attributes[:tags]
|
||||
attributes.delete :tags
|
||||
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)
|
||||
@attributes["starred"] = (params[:new_todo_starred]||"").include? "true" if params[:new_todo_starred]
|
||||
attributes["starred"] = (params[:new_todo_starred]||"").include? "true" if params[:new_todo_starred]
|
||||
end
|
||||
|
||||
def attributes
|
||||
@attributes
|
||||
def find_attributes(params)
|
||||
(params['request'] && params['request']['todo']) || params['todo']
|
||||
end
|
||||
|
||||
def show_from
|
||||
@attributes['show_from']
|
||||
attributes['show_from']
|
||||
end
|
||||
|
||||
def due
|
||||
@attributes['due']
|
||||
attributes['due']
|
||||
end
|
||||
|
||||
def project_name
|
||||
|
|
@ -41,7 +40,7 @@ module Todos
|
|||
end
|
||||
|
||||
def project_id
|
||||
@attributes['project_id']
|
||||
attributes['project_id']
|
||||
end
|
||||
|
||||
def context_name
|
||||
|
|
@ -49,7 +48,7 @@ module Todos
|
|||
end
|
||||
|
||||
def context_id
|
||||
@attributes['context_id']
|
||||
attributes['context_id']
|
||||
end
|
||||
|
||||
def tag_list
|
||||
|
|
@ -80,14 +79,14 @@ module Todos
|
|||
end
|
||||
|
||||
def project_specified_by_name?
|
||||
return false unless @attributes['project_id'].blank?
|
||||
return false unless attributes['project_id'].blank?
|
||||
return false if project_name.blank?
|
||||
return false if project_name == 'None'
|
||||
true
|
||||
end
|
||||
|
||||
def context_specified_by_name?
|
||||
return false unless @attributes['context_id'].blank?
|
||||
return false unless attributes['context_id'].blank?
|
||||
return false if context_name.blank?
|
||||
true
|
||||
end
|
||||
|
|
@ -100,18 +99,18 @@ module Todos
|
|||
|
||||
def find_or_create_group(group_type, set, name)
|
||||
return set_id_by_name(group_type, set, name) if specified_by_name?(group_type)
|
||||
return set_id_by_id_string(group_type, set, @attributes["#{group_type}_id"]) if specified_by_id?(group_type)
|
||||
return set_id_by_id_string(group_type, set, attributes["#{group_type}_id"]) if specified_by_id?(group_type)
|
||||
end
|
||||
|
||||
def set_id_by_name(group_type, set, name)
|
||||
group = set.where(:name => name).first_or_create
|
||||
@attributes["#{group_type}_id"] = group.id
|
||||
attributes["#{group_type}_id"] = group.id
|
||||
return group.new_record_before_save?
|
||||
end
|
||||
|
||||
def set_id_by_id_string(group_type, set, id)
|
||||
# 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
|
||||
# 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
|
||||
return false
|
||||
rescue
|
||||
@errors << { :attribute => group_type, :message => "unknown"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue