Class: Todos::TodoCreateParamsHelper

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/todos/todo_create_params_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, user) ⇒ TodoCreateParamsHelper


6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/todos/todo_create_params_helper.rb', line 6

def initialize(params, user)
  set_params(params)
  filter_attributes(params)
  filter_tags
  filter_starred

  @user = user
  @errors = []

  @new_project_created = find_or_create_group(:project, user.projects, project_name)
  @new_context_created = find_or_create_group(:context, user.contexts, context_name)
end

Instance Attribute Details

#new_context_createdObject (readonly)

Returns the value of attribute new_context_created


4
5
6
# File 'app/controllers/todos/todo_create_params_helper.rb', line 4

def new_context_created
  @new_context_created
end

#new_project_createdObject (readonly)

Returns the value of attribute new_project_created


4
5
6
# File 'app/controllers/todos/todo_create_params_helper.rb', line 4

def new_project_created
  @new_project_created
end

Instance Method Details

#add_errors(model) ⇒ Object


117
118
119
# File 'app/controllers/todos/todo_create_params_helper.rb', line 117

def add_errors(model)
  @errors.each {|e| model.errors[ e[:attribute] ] = e[:message] }
end

#attributesObject


49
50
51
# File 'app/controllers/todos/todo_create_params_helper.rb', line 49

def attributes
  @attributes
end

#context_idObject


73
74
75
# File 'app/controllers/todos/todo_create_params_helper.rb', line 73

def context_id
  @attributes['context_id']
end

#context_nameObject


69
70
71
# File 'app/controllers/todos/todo_create_params_helper.rb', line 69

def context_name
  @params['context_name'].strip unless @params['context_name'].nil?
end

#context_specified_by_name?Boolean


111
112
113
114
115
# File 'app/controllers/todos/todo_create_params_helper.rb', line 111

def context_specified_by_name?
  return false if @attributes['context_id'].present?
  return false if context_name.blank?
  true
end

#dueObject


57
58
59
# File 'app/controllers/todos/todo_create_params_helper.rb', line 57

def due
  @attributes['due']
end

#filter_attributes(params) ⇒ Object


23
24
25
26
27
28
29
30
# File 'app/controllers/todos/todo_create_params_helper.rb', line 23

def filter_attributes(params)
  if params[:request]
    @attributes = todo_params(params[:request])
  elsif params[:todo]
    @attributes = todo_params(params)
  end
  @attributes = {} if @attributes.nil?  # make sure there is at least an empty hash
end

#filter_starredObject


43
44
45
46
47
# File 'app/controllers/todos/todo_create_params_helper.rb', line 43

def filter_starred
  if @params[:new_todo_starred]
    @attributes["starred"] = (@params[:new_todo_starred]||"").include? "true"
  end
end

#filter_tagsObject


32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/todos/todo_create_params_helper.rb', line 32

def filter_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
    # 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
  end
end

#find_or_create_group(group_type, set, name) ⇒ Object (private)


148
149
150
151
# File 'app/controllers/todos/todo_create_params_helper.rb', line 148

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)
end

#parse_datesObject


85
86
87
88
89
# File 'app/controllers/todos/todo_create_params_helper.rb', line 85

def parse_dates()
  @attributes['show_from'] = @user.prefs.parse_date(show_from)
  @attributes['due'] = @user.prefs.parse_date(due)
  @attributes['due'] ||= ''
end

#predecessor_listObject


81
82
83
# File 'app/controllers/todos/todo_create_params_helper.rb', line 81

def predecessor_list
  @params['predecessor_list']
end

#project_idObject


65
66
67
# File 'app/controllers/todos/todo_create_params_helper.rb', line 65

def project_id
  @attributes['project_id']
end

#project_nameObject


61
62
63
# File 'app/controllers/todos/todo_create_params_helper.rb', line 61

def project_name
  @params['project_name'].strip unless @params['project_name'].nil?
end

#project_specified_by_name?Boolean


104
105
106
107
108
109
# File 'app/controllers/todos/todo_create_params_helper.rb', line 104

def project_specified_by_name?
  return false if @attributes['project_id'].present?
  return false if project_name.blank?
  return false if project_name == 'None'
  true
end

#sequential?Boolean


91
92
93
# File 'app/controllers/todos/todo_create_params_helper.rb', line 91

def sequential?
  return @params[:todos_sequential].present? && @params[:todos_sequential]=='true'
end

#set_id_by_id_string(group_type, set, id) ⇒ Object (private)


159
160
161
162
163
164
165
# File 'app/controllers/todos/todo_create_params_helper.rb', line 159

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
  return false
rescue
  @errors << { :attribute => group_type, :message => "unknown"}
end

#set_id_by_name(group_type, set, name) ⇒ Object (private)


153
154
155
156
157
# File 'app/controllers/todos/todo_create_params_helper.rb', line 153

def set_id_by_name(group_type, set, name)
  group = set.where(:name => name).first_or_create
  @attributes["#{group_type}_id"] = group.id
  return group.new_record_before_save?
end

#set_params(params) ⇒ Object


19
20
21
# File 'app/controllers/todos/todo_create_params_helper.rb', line 19

def set_params(params)
  @params = params['request'] || params
end

#show_fromObject


53
54
55
# File 'app/controllers/todos/todo_create_params_helper.rb', line 53

def show_from
  @attributes['show_from']
end

#specified_by_id?(group_type) ⇒ Boolean


99
100
101
102
# File 'app/controllers/todos/todo_create_params_helper.rb', line 99

def specified_by_id?(group_type)
  group_id = send("#{group_type}_id")
  group_id.present?
end

#specified_by_name?(group_type) ⇒ Boolean


95
96
97
# File 'app/controllers/todos/todo_create_params_helper.rb', line 95

def specified_by_name?(group_type)
  return send("#{group_type}_specified_by_name?")
end

#tag_listObject


77
78
79
# File 'app/controllers/todos/todo_create_params_helper.rb', line 77

def tag_list
  @params['tag_list']
end

#todo_params(params) ⇒ Object (private)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/todos/todo_create_params_helper.rb', line 123

def todo_params(params)
  # keep :predecessor_dependencies from being filterd (for XML API).
  # The permit cannot handle multiple precessors
  if params[:todo][:predecessor_dependencies]
    deps = params[:todo][:predecessor_dependencies][:predecessor]
  end

  # accept empty :todo hash
  if params[:todo].empty?
    params[:todo] = {:ignore => true}
  end

  filtered = params.require(:todo).permit(
    :context_id, :project_id, :description, :notes,
    :due, :show_from, :state,
    # XML API
    :tags => [:tag => [:name]],
    :context => [:name],
    :project => [:name])

  # add back :predecessor_dependencies
  filtered[:predecessor_dependencies] = {:predecessor => deps } unless deps.nil?
  filtered
end