Class: PrayersController

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

Overview

The PrayersController handles all forward-facing prayer request related pages and methods on the site.

Author:

  • Robert Klubenspies

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#createObject

Creates new prayer requests through a Rails remote form (UJS) post. Renders UJS response located in app/views/prayers/create.js.erb

Author:

  • Robert Klubenspies

Since:

  • 0.1.0


28
29
30
31
32
33
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/prayers_controller.rb', line 28

def create
  # Find or create Email entry for email address on form and assign it to instance variable @email
  @email = Email.find_or_create_by(email: params[:prayer][:email])
  
  # Remove email address from form parameters (for saftey reasons - don't want the email address showing up elseware)
  params[:prayer].delete :email
  
  # Assign checkbox "Would you like to receive a link to..." value to @email_me instance variable
  @email_me = params[:prayer][:email_me]
  
  # Remove checkbox value from form parameters (for saftey reasons - don't want user's decision showing up elseware)
  params[:prayer].delete :email_me
  
  # Create form parameter for User's IP Address
  params[:prayer][:ip_address] = request.env['REMOTE_ADDR']
  
  # Ensure location data is correct
  if params[:prayer][:location] != "Anonymous"
    l = Geocoder.search(params[:prayer][:location])
    params[:prayer][:location] = l[0].address
  elsif params[:prayer][:location] == "Anonymous"
    params[:prayer][:location] = nil
  end
  
  # Intialize Prayer object and "build" it under Email object persisted in @email instance variable
  @prayer = @email.prayers.build(params[:prayer])
  
  # Save Prayer object
  if @prayer.save
    # If user chose to have link emailed to them, email it
    if @email_me == "1"
      PrayerMailer.permalink_email(@prayer).deliver     # Email user permalink
    # User doesn't want to have link emailed to them
    else
      puts "User chose not to have permalink emailed."  # Log that user did not want to receive email permalink
    end
  end
end

#indexObject

Displays homepage with three tabs: Prayer Requests, Submit a Prayer Request, About Share a Prayer

Author:

  • Robert Klubenspies

Since:

  • 0.1.0


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/prayers_controller.rb', line 10

def index
  # Determine when the oldest prayer's timestamp from params[:last] OR use the current time + 1 second
  last = params[:last].blank? ? Time.now.utc + 1.second : Time.parse(params[:last])
  
  # Retrieve 10 prayer requests based on the timestamp
  @prayers = Prayer.feed(last)

  # Respond to multiple request types
  respond_to do |format|
    format.js   # Respond with UJS to dynamically add to list on homepage (app/views/prayers/index.js.erb)
    format.html # Respond with HTML view (app/views/prayers/index.html.haml)
  end
end

#prayed_forObject

Proccesses a prayer being prayed for

Author:

  • Robert Klubenspies

Since:

  • 0.1.0


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/prayers_controller.rb', line 87

def prayed_for
  # Determine whether or not the prayer request was already prayed for
  if (session[:prayed_for] ||= []).include? params[:id]
    return false
  # Prayer request was not already prayed for, proceed as usual
  else
    # Find Prayer by params[:id]
    @prayer = Prayer.find(params[:id])
  
    # Increment :time_prayed_for count in database row
    @prayer.inc(:times_prayed_for, 1)
  
    # Check to make sure updates were saved
    if @prayer.save
      (session[:prayed_for] ||= []) << params[:id]  # Add string representation of prayer's id to session array of prayer requests that were prayed for (for making sure user doesn't press button again by accident)
    end
  
    # Respond to one request type
    respond_to do |format|
      format.js   # Respond with UJS to manipulate DOM (app/views/prayers/prayed_for.js.erb)
    end
  end
end

#reportObject

Proccesses a prayer being reported

Author:

  • Robert Klubenspies

Since:

  • 0.1.0


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

def report
  # Determine whether or not the prayer request was already reported
  if (session[:reported] ||= []).include? params[:id]
    return false
  # Prayer request was not already reported for, proceed as usual
  else
    # Find Prayer by params[:id]
    @prayer = Prayer.find(params[:id])
  
    # If prayer request was already reported 2 or more times, then destroy the prayer request
    if @prayer.reported >= 2
      @prayer.destroy
    # Prayer request need to be reported more times before being destroyed, increment reported count
    else
      # Increment :reported count in database row
      @prayer.inc(:reported, 1)
    
      # Check to make sure updates were saved
      if @prayer.save
        (session[:reported] ||= []) << params[:id]  # Add string representation of prayer's id to session array of prayer requests that were reported (for making sure user doesn't press button again by accident)
      end
    end
  
    # Respond to one request type
    respond_to do |format|
      format.js   # Respond with UJS to manipulate DOM (app/views/prayers/report.js.erb)
    end
  end
end

#showObject

Displays a single prayer request's permalinked page based on its id (found in params and passed by the route)


72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/prayers_controller.rb', line 72

def show
  # Retrieve prayer request by params[:id]
  @prayer = Prayer.find(params[:id])
  
  # Determine whether or not the prayer request was already prayed for
  @already_prayed_for = (session[:prayed_for] ||= []).include? params[:id]
  
  # Determine whether or not the prayer request was already reported
  @already_reported = (session[:reported] ||= []).include? params[:id]
end