Class: TasksController

Inherits:
ApplicationController show all
Includes:
FormAjaxHelper
Defined in:
app/controllers/tasks_controller.rb

Instance Method Summary collapse

Methods included from FormAjaxHelper

#format_errors

Methods included from SessionsHelper

#preferences_customer_type=, #preferences_customer_type?, #signed_in?, #signed_in_user, #store_location

Constructor Details

#initializeTasksController

Returns a new instance of TasksController.



8
9
10
11
# File 'app/controllers/tasks_controller.rb', line 8

def initialize()
  super
  @no_layout = false
end

Instance Method Details

#createObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/tasks_controller.rb', line 77

def create
  load_form
  
  respond_to do |format|
    if @task.save
      format.js { render :locals => { :task => @task }, :layout => false, :content_type => "application/javascript", :status => :created }
      format.html { redirect_to [@task.interested, @task] }
    else
      format.js { render :json => format_errors("tasks", @task.errors), :content_type => "application/json", :status => :unprocessable_entity }
      format.html { render :action => :new, :status => :unprocessable_entity }
    end
  end
end

#editObject



40
41
42
# File 'app/controllers/tasks_controller.rb', line 40

def edit
  @task.feedbacks.build
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/tasks_controller.rb', line 13

def index
  respond_to do |format|
    format.html { render "index", :layout => "application.html.erb" }
    format.json do
       #@content = render_to_string( :template => "tasks/_list", :locals => { items: @customer.tasks.paginate(page:params[:task_page] || 1, per_page: 3) }, :formats => :html, :layout => false)
       #render :json => { html: @content }, :content_type => "application/json"
       #render :json => @content, :content_type => "application/json"
       render :template => "tasks/_list", :locals => { items: @customer.tasks.paginate(page:params[:task_page] || 1, per_page: 3) }, :formats => :html, :layout => false
    end
    format.js { head :no_content }
  end
end

#load_formObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/tasks_controller.rb', line 44

def load_form
  #remove conficts
  feedback_params = params[:task][:feedbacks]
  params[:task].delete :feedbacks
  
  #not loaded task for incompatibilities
  if params[:id]
    @task = @customer.tasks.find params[:id]
    @task.update_attributes(params[:task])
  else
    @task = @customer.tasks.build(params[:task])
  end
  @task.feedbacks.build(feedback_params)
  
  @task.status = SystemTaskStatus.OPENED
  @task.user = current_user
  
  #filled feedback?
  if (@task.feedbacks.size == 1)
    feedback = @task.feedbacks[0]
    if @task.feedbacks[0].valid?
      #TODO: Business Rule. Put in model
      @task.resolution = feedback.resolution
      @task.status = SystemTaskStatus.CLOSED
      @task.finish_time = Time.now
    else
      if (@task.feedbacks[0].resolution.nil? && @task.feedbacks[0].notes.empty?)
        @task.feedbacks.delete(@task.feedbacks[0])
      end
    end
  end
end

#newObject



36
37
38
# File 'app/controllers/tasks_controller.rb', line 36

def new
  @task.feedbacks.build
end

#showObject



26
27
28
29
30
31
32
33
34
# File 'app/controllers/tasks_controller.rb', line 26

def show
  
  @feedbacks = @task.feedbacks
  
  respond_to do |format| 
    format.html #
    format.js { @no_layout = true }
  end
end

#updateObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/tasks_controller.rb', line 91

def update
  load_form
  
  
  respond_to do |format|
    
    if @task.update_attributes(params[:task])
      format.html { redirect_to([@task.interested, @task], :notice => I18n.t('forms.update.sucess')) }
      format.json { head :ok }
    else
      format.html { render :action => "edit" }
      format.json { render :json => @task.errors, :status => :unprocessable_entity }
    end
  end
end