Class: ContextsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/contexts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_login_required, #all_done_todos_for, #boolean_param, cas_enabled?, #cas_enabled?, #count_deferred_todos, #count_undone_todos, #count_undone_todos_phrase, #done_todos_for, #enable_mobile_content_negotiation, #for_autocomplete, #format_date, #format_dependencies_as_json_for_auto_complete, #handle_unverified_request, #init_data_for_sidebar, #init_not_done_counts, #init_project_hidden_todo_counts, #mobile?, #notify, #openid_enabled?, openid_enabled?, #parse_date_per_user_prefs, #prefered_auth?, prefered_auth?, #redirect_back_or_home, #render_failure, #sanitize, #set_charset, #set_group_view_by, #set_locale, #set_session_expiration, #set_time_zone, #set_zindex_counter, #todo_xml_params

Methods included from LoginSystem

#access_denied, #authorize?, #basic_auth_denied, #current_user, #get_basic_auth_data, #get_current_user, #logged_in?, #login_from_cookie, #login_optional, #login_or_feed_token_required, #login_required, #logout_user, #prefs, #protect?, #redirect_back_or_default, #redirect_to_login, #set_current_user, #store_location

Instance Method Details

#all_done_todosObject


160
161
162
# File 'app/controllers/contexts_controller.rb', line 160

def all_done_todos
  all_done_todos_for current_user.contexts.find(params[:id])
end

#context_paramsObject (private)


166
167
168
# File 'app/controllers/contexts_controller.rb', line 166

def context_params
  params.require(:context).permit(:name, :position, :state)
end

#createObject


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/contexts_controller.rb', line 67

def create
  if params[:format] == 'application/xml' && params['exception']
    render_failure "Expected post format is valid xml like so: <context><name>context name</name></context>.", 400
    return
  end
  @context = current_user.contexts.build(context_params)
  @context.hide! if params['context_state'] && params['context_state']['hide'] == '1'
  @saved = @context.save
  @context_not_done_counts = { @context.id => 0 }
  respond_to do |format|
    format.js do
      @down_count = current_user.contexts.size
      init_not_done_counts
    end
    format.xml do
      if @context.new_record?
        render_failure @context.errors.to_xml.html_safe, 409
      else
        head :created, :location => context_url(@context)
      end
    end
  end
end

#destroyObject

Fairly self-explanatory; deletes the context If the context contains actions, you’ll get a warning dialogue. If you choose to go ahead, any actions in the context will also be deleted.


131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/contexts_controller.rb', line 131

def destroy
  # make sure the deleted recurring patterns are removed from associated todos
  @context.recurring_todos.each { |rt| rt.clear_todos_association } unless @context.recurring_todos.nil?

  @context.destroy
  respond_to do |format|
    format.js do
      @down_count = current_user.contexts.size
      update_state_counts
    end
    format.xml { render :text => "Deleted context #{@context.name}" }
  end
end

#done_todosObject


156
157
158
# File 'app/controllers/contexts_controller.rb', line 156

def done_todos
  done_todos_for current_user.contexts.find(params[:id])
end

#editObject


121
122
123
124
125
126
# File 'app/controllers/contexts_controller.rb', line 121

def edit
  @context = Context.find(params[:id])
  respond_to do |format|
    format.js
  end
end

#feed_optionsObject (protected)


225
226
227
# File 'app/controllers/contexts_controller.rb', line 225

def feed_options
  Context.feed_options(current_user)
end

#indexObject


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/contexts_controller.rb', line 10

def index
  @all_contexts    = current_user.contexts
  @active_contexts = current_user.contexts.active
  @hidden_contexts = current_user.contexts.hidden
  @closed_contexts = current_user.contexts.closed
  init_not_done_counts(['context']) unless request.format == :autocomplete

  respond_to do |format|
    format.html &render_contexts_html
    format.m    &render_contexts_mobile
    format.xml  { render :xml => @all_contexts.to_xml( :except => :user_id ) }
    format.any(:rss, :atom) do
      @feed_title = 'Tracks Contexts'
      @feed_description = "Lists all the contexts for #{current_user.display_name}"
    end
    format.text do
      # somehow passing Mime::TEXT using content_type to render does not work
      headers['Content-Type']=Mime::TEXT.to_s
      render :action => 'index', :layout => false, :content_type => Mime::TEXT
    end
    format.autocomplete &render_autocomplete
  end
end

#initObject (protected)


235
236
237
238
# File 'app/controllers/contexts_controller.rb', line 235

def init
  @source_view = params['_source_view'] || 'context'
  init_data_for_sidebar
end

#orderObject

Methods for changing the sort order of the contexts in the list


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

def order
  context_ids = params["container_context"]
  @projects = current_user.contexts.update_positions( context_ids )
  render :nothing => true
rescue
  notify :error, $!
  redirect_to :action => 'index'
end

#process_params_for_updateObject (protected)


240
241
242
243
244
245
246
247
248
249
250
# File 'app/controllers/contexts_controller.rb', line 240

def process_params_for_update
  params['context'] ||= {}
  @success_text = if params['field'] == 'name' && params['value']
    params['context']['id'] = params['id']
    params['context']['name'] = params['value']
  end

  @original_context_state = @context.state
  @new_state = params['context']['state']
  params['context'].delete('state')
end

#render_autocompleteObject (protected)


219
220
221
222
223
# File 'app/controllers/contexts_controller.rb', line 219

def render_autocomplete
  lambda do
    render :text => for_autocomplete(current_user.contexts, params[:term])
  end
end

#render_context_mobileObject (protected)


208
209
210
211
212
213
214
215
216
217
# File 'app/controllers/contexts_controller.rb', line 208

def render_context_mobile
  lambda do
    @page_title = "TRACKS::List actions in "+@context.name
    @not_done = @not_done_todos.select {|t| t.context_id == @context.id }
    @down_count = @not_done.size
    cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
    @mobile_from_context = @context.id
    render
  end
end

#render_contexts_htmlObject (protected)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/contexts_controller.rb', line 181

def render_contexts_html
  lambda do
    @page_title = "TRACKS::List Contexts"
    @no_active_contexts = @active_contexts.empty?
    @no_hidden_contexts = @hidden_contexts.empty?
    @no_closed_contexts = @closed_contexts.empty?
    @active_count = @active_contexts.size
    @hidden_count = @hidden_contexts.size
    @closed_count = @closed_contexts.size
    @count = @active_count + @hidden_count + @closed_count
    @new_context = current_user.contexts.build

    render
  end
end

#render_contexts_mobileObject (protected)


197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/contexts_controller.rb', line 197

def render_contexts_mobile
  lambda do
    @page_title = "TRACKS::List Contexts"
    @active_contexts = current_user.contexts.active
    @hidden_contexts = current_user.contexts.hidden
    @down_count = @active_contexts.size + @hidden_contexts.size
    cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
    render
  end
end

#set_context_from_paramsObject (protected)


229
230
231
232
233
# File 'app/controllers/contexts_controller.rb', line 229

def set_context_from_params
  @context = current_user.contexts.find(params[:id])
rescue
  @context = nil
end

#set_state_for_update(new_state) ⇒ Object (protected)


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/contexts_controller.rb', line 252

def set_state_for_update(new_state)
  begin
    unless @original_context_state == new_state
      if new_state == 'active'
        @context.activate!
      elsif new_state == 'hidden'
        @context.hide!
      elsif new_state == 'closed'
        @context.close!
      end
    end
    return true
  rescue AASM::InvalidTransition
    @context.errors.add(:state, "cannot be changed. The context cannot be closed if you have uncompleted actions in this context")
    return false
  end
end

#showObject


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/contexts_controller.rb', line 34

def show
  set_context_from_params

  unless @context.nil?
    @max_completed = current_user.prefs.show_number_completed
    @done = @context.todos.completed.limit(@max_completed).reorder("todos.completed_at DESC, todos.created_at DESC").includes(Todo::DEFAULT_INCLUDES)
    @not_done_todos = @context.todos.active.reorder("todos.due IS NULL, todos.due ASC, todos.created_at ASC").includes(Todo::DEFAULT_INCLUDES)
    @todos_without_project = @not_done_todos.select{|t| t.project.nil?}

    @deferred_todos = @context.todos.deferred.includes(Todo::DEFAULT_INCLUDES)
    @pending_todos = @context.todos.pending.includes(Todo::DEFAULT_INCLUDES)

    @projects = current_user.projects
    @contexts = current_user.contexts

    @projects_to_show = @projects.active
    @contexts_to_show = [@context]

    @count = @not_done_todos.count + @deferred_todos.count + @pending_todos.count
    @page_title = "TRACKS::Context: #{@context.name}"
    respond_to do |format|
      format.html
      format.m    &render_context_mobile
      format.xml  { render :xml => @context.to_xml( :except => :user_id ) }
    end
  else
    respond_to do |format|
      format.html { render :text => 'Context not found', :status => 404 }
      format.xml  { render :xml => '<error>Context not found</error>', :status => 404 }
    end
  end
end

#updateObject

Edit the details of the context


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/contexts_controller.rb', line 93

def update
  process_params_for_update

  @context.attributes = context_params
  @saved = @context.save
  @state_saved = set_state_for_update(@new_state)
  @saved = @saved && @state_saved

  if @saved
    @state_changed = (@original_context_state != @context.state)
    @new_state = @context.state if @state_changed
    @active_contexts = current_user.contexts.active
    @hidden_contexts = current_user.contexts.hidden
    @closed_contexts = current_user.contexts.closed
  end

  respond_to do |format|
    format.js
    format.xml {
        if @saved
          render :xml => @context.to_xml( :except => :user_id )
        else
          render :text => "Error on update: #{@context.errors.full_messages.inject("") {|v, e| v + e + " " }}", :status => 409
        end
      }
  end
end

#update_state_countsObject (protected)


172
173
174
175
176
177
178
179
# File 'app/controllers/contexts_controller.rb', line 172

def update_state_counts
  @active_contexts_count = current_user.contexts.active.count
  @hidden_contexts_count = current_user.contexts.hidden.count
  @closed_contexts_count = current_user.contexts.closed.count
  @show_active_contexts = @active_contexts_count > 0
  @show_hidden_contexts = @hidden_contexts_count > 0
  @show_closed_contexts = @closed_contexts_count > 0
end