Class: Mg::GoalsController

Inherits:
Mg
  • Object
show all
Defined in:
lib/mountain-goat/controllers/mg/goals_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /mg/goals POST /mg/goals.xml



71
72
73
74
75
76
77
78
79
80
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 71

def create
  @goal = Mg::Goal.new(params[:goal])

  if @goal.save
    flash[:notice] = 'Goal was successfully created.'
    redirect_to mg_goal_url :id => @goal.id
  else
    render :action => "new"
  end
end

#destroyObject

DELETE /mg/goals/1 DELETE /mg/goals/1.xml



124
125
126
127
128
129
130
131
132
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 124

def destroy
  @goal = Mg::Goal.find(params[:id])
  @goal.destroy

  respond_to do |format|
    format.html { redirect_to mg_goals_url }
    format.xml  { head :ok }
  end
end

#editObject

GET /mg/goals/1/edit



65
66
67
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 65

def edit
  @goal = Mg::Goal.find(params[:id])
end

#hideObject

GET /mg/goals/1/hide GET /mg/goals/1/hide.xml



98
99
100
101
102
103
104
105
106
107
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 98

def hide
  @goal = Mg::Goal.find(params[:id])
  @goal.update_attribute(:is_hidden, true)
  flash[:notice] = "Goal #{@goal.name} has been hidden."
  
  respond_to do |format|
    format.html { redirect_to mg_goals_url }
    format.xml  { head :ok }
  end
end

#indexObject

GET /mg/goal GET /mg/goals.xml



6
7
8
9
10
11
12
13
14
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 6

def index
  @goals = Mg::Goal.all(:conditions => { :is_hidden => false } )
  @hidden_goals = Mg::Goal.all(:conditions => { :is_hidden => true } )

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @goals }
  end
end

#newObject

GET /mg/goals/new GET /mg/goals/new.xml



55
56
57
58
59
60
61
62
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 55

def new
  @goal = Mg::Goal.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @goal }
  end
end

#showObject

GET /mg/goals/1 GET /mg/goals/1.xml



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 18

def show
  @goal = Mg::Goal.find(params[:id])

  @results_per_day = []
  created_at = @goal.created_at
  running_date = Time.utc( created_at.year, created_at.month, created_at.day )
  
  while running_date < Time.zone.now
    @results_per_day.push({ :date => running_date.to_i * 1000, :val => @goal.mg_records.find( :all, :conditions => { :created_at => running_date..(running_date + 60 * 60 * 24) } ).count })
    running_date += 60 * 60 * 24
  end
  
  @results_by_gmt = {}
  
  @goal.mg_goal_meta_types.each do |gmt|
    @results_by_gmt[gmt.id] = []
    
    gmt.meta.all(:select => "data, count(*) as count", :group => "data").each do |meta|
      next if meta.data.nil?
      if gmt.meta_type == 'cs_meta' || gmt.meta_type == 'gs_meta' 
        @results_by_gmt[gmt.id].push( { :name => meta.data, :val => meta.count, :title => meta.data } )
        #@results_by_gmt_titles[gmt.id].merge!({ i => meta.data })
      else
        @results_by_gmt[gmt.id].push( { :name => meta.data, :val => meta.count, :title => meta.data } )
      end
      
    end
  end
  
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @goal }
  end
end

#unhideObject

GET /mg/goals/1/unhide GET /mg/goals/1/unhide.xml



111
112
113
114
115
116
117
118
119
120
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 111

def unhide
  @goal = Mg::Goal.find(params[:id])
  @goal.update_attribute(:is_hidden, false)
  flash[:notice] = "Goal #{@goal.name} has been restored."
  
  respond_to do |format|
    format.html { redirect_to mg_goals_url }
    format.xml  { head :ok }
  end
end

#updateObject

PUT /goals/1 PUT /goals/1.xml



84
85
86
87
88
89
90
91
92
93
# File 'lib/mountain-goat/controllers/mg/goals_controller.rb', line 84

def update
  @goal = Mg::Goal.find(params[:id])

  if @goal.update_attributes(params[:goal])
    flash[:notice] = 'Goal was successfully updated.'
    redirect_to mg_goal_url :id => @goal.id
  else
    render :action => "edit"
  end
end