Class: Todos::TodoCreateParamsHelper
- Inherits:
-
Object
- Object
- Todos::TodoCreateParamsHelper
- Defined in:
- app/controllers/todos/todo_create_params_helper.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#new_context_created ⇒ Object
readonly
Returns the value of attribute new_context_created.
-
#new_project_created ⇒ Object
readonly
Returns the value of attribute new_project_created.
Instance Method Summary collapse
- #add_errors(model) ⇒ Object
- #context_id ⇒ Object
- #context_name ⇒ Object
- #context_specified_by_name? ⇒ Boolean
- #due ⇒ Object
- #filter_attributes(params) ⇒ Object
- #filter_starred ⇒ Object
- #filter_tags ⇒ Object
- #find_or_create_group(group_type, set, name) ⇒ Object private
-
#initialize(params, user) ⇒ TodoCreateParamsHelper
constructor
A new instance of TodoCreateParamsHelper.
- #parse_dates ⇒ Object
- #predecessor_list ⇒ Object
- #project_id ⇒ Object
- #project_name ⇒ Object
- #project_specified_by_name? ⇒ Boolean
- #sequential? ⇒ Boolean
- #set_id_by_id_string(group_type, set, id) ⇒ Object private
- #set_id_by_name(group_type, set, name) ⇒ Object private
- #set_params(params) ⇒ Object
- #show_from ⇒ Object
- #specified_by_id?(group_type) ⇒ Boolean
- #specified_by_name?(group_type) ⇒ Boolean
- #tag_list ⇒ Object
- #todo_params(params) ⇒ Object private
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_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
#attributes ⇒ Object (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_created ⇒ Object (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_created ⇒ Object (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_id ⇒ Object
69 70 71 |
# File 'app/controllers/todos/todo_create_params_helper.rb', line 69 def context_id @attributes['context_id'] end |
#context_name ⇒ Object
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
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 |
#due ⇒ Object
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_starred ⇒ Object
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_tags ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'app/controllers/todos/todo_create_params_helper.rb', line 32 def 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_dates ⇒ Object
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_list ⇒ Object
77 78 79 |
# File 'app/controllers/todos/todo_create_params_helper.rb', line 77 def predecessor_list @params['predecessor_list'] end |
#project_id ⇒ Object
61 62 63 |
# File 'app/controllers/todos/todo_create_params_helper.rb', line 61 def project_id @attributes['project_id'] end |
#project_name ⇒ Object
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
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
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_from ⇒ Object
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
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
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_list ⇒ Object
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 |