Skip to content

Instantly share code, notes, and snippets.

@soilforlifeforms
Created July 17, 2015 09:26
Show Gist options
  • Save soilforlifeforms/09d3705422bb4ffa9086 to your computer and use it in GitHub Desktop.
Save soilforlifeforms/09d3705422bb4ffa9086 to your computer and use it in GitHub Desktop.
class Gardener < ActiveRecord::Base
belongs_to :group
has_many :answers
#has_and_belongs_to_many :questionnaires, join_table: 'gardener_questionnaires'
#has_and_belongs_to_many :questionnaires, join_table: 'gardener_questionnaires'
#has_many :answers, through: :gardener_questionnaires
has_many :attendance_registers_training_sessions
has_many :attendance_registers, through: :attendance_registers_training_sessions
has_many :support_visits, dependent: :destroy
has_one :garden, dependent: :destroy
has_one :living_arrangement, dependent: :destroy
has_one :sfl, dependent: :destroy
has_one :education, dependent: :destroy
has_one :employment, dependent: :destroy
has_one :grant, dependent: :destroy
has_one :ability, dependent: :destroy
has_one :experience, dependent: :destroy
has_one :health, dependent: :destroy
has_one :general_statement, dependent: :destroy
has_one :expenditure, dependent: :destroy
has_many :follow_up_visits
#has_many :documents
# has_attached_file :avatar, :styles => {:thumb => "100x100>"}
# validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
has_attached_file :avatar, dependent: :destroy,
:storage => :dropbox,
:dropbox_credentials => "#{Rails.root}/config/dropbox.yml",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/images/missing_pic.jpeg",
:dropbox_options => {
:path => proc { |style| "#{style}/#{id}_#{avatar.original_filename}"},
:unique_filename => true
}
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates :first_name, length: {minimum: 2, too_short: "First name needs at least 2 characters"}
validates :last_name , length: {minimum: 2, too_short: "Last name needs at least 2 characters"}
validates :contact_number, length: {minimum: 9, too_short: "Contact numbers need to have 10 characters", maximum: 11, too_long: "Contact numbers need to have 10 characters" }
validates :address , length: {minimum: 4, too_short: "Address needs at least 4 characters"}
validates :id_number, :length => { :is => 13,
:too_short => "must have at least %{count} words",
:too_long => "must have at most %{count} words"},
:uniqueness => true
validates :race, :presence => true
has_attached_file :resume, dependent: :destroy,
:storage => :dropbox,
:dropbox_credentials => "#{Rails.root}/config/dropbox.yml"
end
#mount_uploader :image, ImageUploader
def active?
status == 'active'
end
class GardenersController < ApplicationController
# GET /groups/:group_id/gardeners
# GET /groups/:group_id/gardeners.xml
def index
#1st you retrieve the group thanks to params[:group_id]
@group = Group.find(params[:group_id])
#2nd you get all the gardeners of this group
@gardeners = @group.gardeners.order("first_name")
respond_to do |format|
format.html # index.html.erb
format.xml {remder :xml => @gardeners}
end
end
def show
#1st you retrieve the group thanks to params[:group_id]
@group = Group.find(params[:group_id])
#2nd you retrieve the comment thanks to params[:id]
@gardener = @group.gardeners.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml {render :xml => @gardener}
end
end
# GET /groups/:group_id/gardeners/new
# GET /groups/:group_id/gardeners/new.xml
def new
#1st you retrieve the group thanks to params[:group_id]
@group= Group.find(params[:group_id])
#2nd you build a new comment
@gardener = @group.gardeners.build
respond_to do |format|
format.html #new.html.erb
format.xml {render :xml => @gardener }
end
end
# GET /groups/:group_id/gardeners/:id/edit
def edit
#1st you retrieve the group thanks to params[:group_id]
@group = Group.find(params[:group_id])
#2nd you retrieve the comment thanks to params[:id]
@gardener = @group.gardeners.find(params[:id])
end
# POST /groups/:group_id/group_gardeners
# POST /groups/:group_id/group_gardeners.xml
def create
#1st you retrieve the group thanks to params[:group_id]
@group = Group.find(params[:group_id])
#2nd you create the trainer wih arguments in params [:gardener]
@gardener = @group.gardeners.new(gardener_params)
respond_to do |format|
if @gardener.save
#1st argument of redirect_to is an array, in order to build the correct route to the nested resource gardener
format.html {redirect_to([@gardener.group], :notice => 'Home Gardener was successfully created' )}
format.xml {render :xml => @gardener, :status => :created, :location => [@gardener.group,@gardener] }
else
format.html {render :action => "new"}
format.xml {render :xml => @gardener.errors, status: :unprocessable_entity}
end
end
end
def update
#1st you retrieve the group thanks to params[:group_id]
@group = Group.find(params[:group_id])
@gardener = @group.gardeners.find(params[:id])
respond_to do |format|
if @gardener.update_attributes(gardener_params)
#1st argument of redirect_to is an array, in order to build the correct route to the nested resource gardener
format.html {redirect_to([@gardener.group, @gardener], :notice => 'Gardener was successfully updated')}
format.xml { head :ok}
else
format.html {render :action => "edit"}
format.xml {render :xml => @gardener.errors, :status => :unprocessable_entity}
end
end
end
def destroy
#1st you retrieve the group thanks to params[:group_id]
@group= Group.find(params[:group_id])
#2nd you retrieve the gardener thanks to params[:id]
@gardener = @group.gardeners.find(params[:id])
@gardener.destroy
respond_to do |format|
#1st argument reference the path /groups/:group_id/gardeners
format.html {redirect_to(group_gardeners_url)}
format.xml {head :ok}
end
end
private
def gardener_params
params.require(:gardener).permit(:first_name, :race, :last_name, :contact_number, :address, :group_id, :garden_at_home, :document, :id_number, :avatar)
end
def group_params
params.require(:group).permit(:name, :area, :group_id)
end
end
Jul 17 02:24:57 sflforms app/web.1: Processing by GardenersController#show as HTML
Jul 17 02:24:57 sflforms app/web.1: Started GET "/groups/22/gardeners/55" for 105.210.113.48 at 2015-07-17 09:24:57 +0000
Jul 17 02:24:57 sflforms app/web.1: Parameters: {"group_id"=>"22", "id"=>"55"}
Jul 17 02:24:57 sflforms app/web.1: Rendered gardeners/show.html.erb within layouts/application (14.9ms)
Jul 17 02:24:57 sflforms app/web.1: ActionView::Template::Error (wrong number of arguments (1 for 0)):
Jul 17 02:24:57 sflforms app/web.1: 25: <td><%= @gardener.address %></td>
Jul 17 02:24:57 sflforms app/web.1: 26: <td><%= @gardener.contact_number %></td>
Jul 17 02:24:57 sflforms app/web.1: 27: <% if @gardener.support_visits.exists? %>
Jul 17 02:24:57 sflforms app/web.1: 28: <td><%= @gardener.support_visits.count %> visit complted. Last visit was <%= @gardener.support_visits.last.created_at.to_s(:short) %> </td>
Jul 17 02:24:57 sflforms app/web.1: 29: <% end %>
Jul 17 02:24:57 sflforms heroku/router: at=info method=GET path="/groups/22/gardeners/55" host=sflforms.herokuapp.com request_id=fb70a8cd-f16e-48d8-b33c-5814b2b95a33 fwd="105.210.113.48" dyno=web.1 connect=1ms service=120ms status=500 bytes=1669
Jul 17 02:24:57 sflforms app/web.1: 30: <% if @gardener.follow_up_visits.exists? %>
Jul 17 02:24:57 sflforms app/web.1: 31: <td><%= @gardener.follow_up_visits.last.created_at.to_s(:short) %> </td>
Jul 17 02:24:57 sflforms app/web.1: app/views/gardeners/show.html.erb:28:in `to_s'
Jul 17 02:24:57 sflforms app/web.1: app/views/gardeners/show.html.erb:28:in
<div class="panel panel-default">
<strong>Home</strong>
</p>
<table class="table">
<thead>
<tr>
<th> </th>
<th> First Name </th>
<th> Address</th>
<th> Contact Number</th>
<% if @gardener.support_visits.exists? %>
<th> No. Support Visits & date </th>
<% end %>
<% if @gardener.follow_up_visits.exists? %>
<th> Last Follow Up Visit </th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<td><%= image_tag @gardener.avatar.url(:thumb), :class => "img-circle" %></td>
<td><%= @gardener.first_name %></td>
<td><%= @gardener.address %></td>
<td><%= @gardener.contact_number %></td>
<% if @gardener.support_visits.exists? %>
<td><%= @gardener.support_visits.count %> visit complted. Last visit was <%= @gardener.support_visits.last.created_at.to_s(:short) %> </td>
<% end %>
<% if @gardener.follow_up_visits.exists? %>
<td><%= @gardener.follow_up_visits.last.created_at.to_s(:short) %> </td>
<% end %>
<td><%= link_to 'Edit', edit_group_gardener_path(@gardener.group, @gardener), class: "btn btn-sm btn-success" %></td>
<% if @gardener.garden.nil? %>
<%= link_to 'Begin Initial Questionnaire', new_group_gardener_garden_path(@gardener.group, @gardener), class: "btn btn-sm btn-success" %>
<% end %>
<%= link_to 'Record Support Visit', new_group_gardener_support_visit_path(@gardener.group, @gardener), class: "btn btn-sm btn-info" %>
<%= link_to 'Begin Follow Up Visit', new_follow_up_visit_path(:gardener_id => @gardener.id), class: "btn btn-sm btn-success" %>
</tr>
</tbody>
</table>
<%= link_to 'Back', groups_path, class: "btn btn-sm btn-danger" %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment