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

Returns a new instance of TodoCreateParamsHelper.



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

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

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#new_context_createdObject (readonly)

Returns the value of attribute new_context_created.



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

def new_context_created
  @new_context_created
end

#new_project_createdObject (readonly)

Returns the value of attribute new_project_created.



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

def new_project_created
  @new_project_created
end

Instance Method Details

#add_errors(model) ⇒ Object



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

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

#context_idObject



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

def context_id
  @attributes['context_id']
end

#context_nameObject



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

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

#context_specified_by_name?Boolean

Returns:

  • (Boolean)


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

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

#dueObject



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

def due
  @attributes['due']
end

#filter_attributes(params) ⇒ Object



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

def filter_attributes(params)
  if params[:request]
    @attributes = todo_params(params[:request])
  elsif params[:todo]
    @attributes = todo_params(params)
  end
  # Make sure there is at least an empty hash
  @attributes = {} if @attributes.nil?
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)



143
144
145
146
# File 'app/controllers/todos/todo_create_params_helper.rb', line 143

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



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

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

#predecessor_listObject



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

def predecessor_list
  @params['predecessor_list']
end

#project_idObject



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

def project_id
  @attributes['project_id']
end

#project_nameObject



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

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

#project_specified_by_name?Boolean

Returns:

  • (Boolean)


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

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

#sequential?Boolean

Returns:

  • (Boolean)


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

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

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



156
157
158
159
160
161
162
# File 'app/controllers/todos/todo_create_params_helper.rb', line 156

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)



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

def set_id_by_name(group_type, set, name)
  group = set.where(:name => name).first_or_initialize
  group_is_new = group.new_record?
  group.save if group_is_new
  @attributes["#{group_type}_id"] = group.id
  group_is_new
end

#set_params(params) ⇒ Object



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

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

#show_fromObject



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

def show_from
  @attributes['show_from']
end

#specified_by_id?(group_type) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#specified_by_name?(group_type) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#tag_listObject



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

def tag_list
  @params['tag_list']
end

#todo_params(params) ⇒ Object (private)



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/todos/todo_create_params_helper.rb', line 118

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