You are on page 1of 5

crop.

rb------------------ Object Representation for Crop Database


class Crop < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
has_many :scientific_names, after_add: :update_index, after_remove: :update_in
dex
accepts_nested_attributes_for :scientific_names,
allow_destroy: true,
reject_if: :all_blank
has_many :alternate_names, after_add: :update_index, after_remove: :update_ind
ex, dependent: :destroy
has_many :plantings
has_many :photos, through: :plantings
has_many :seeds
has_many :harvests
has_many :plant_parts, -> { uniq }, through: :harvests
belongs_to :creator, class_name: 'Member'
belongs_to :requester, class_name: 'Member'
belongs_to :parent, class_name: 'Crop'
has_many :varieties, class_name: 'Crop', foreign_key: 'parent_id'
has_and_belongs_to_many :posts # rubocop:disable Rails/HasAndBelongsToMany
before_destroy { |crop| crop.posts.clear }
default_scope { order("lower(name) asc") }
scope :recent, lambda {
where(approval_status: "approved").reorder("created_at desc")
}
scope :toplevel, lambda {
where(approval_status: "approved", parent_id: nil)
}
scope :popular, lambda {
where(approval_status: "approved").reorder("plantings_count desc, lower(name
) asc")
}
scope :randomized, lambda {
# ok on sqlite and psql, but not on mysql
where(approval_status: "approved").reorder('random()')
}
scope :pending_approval, -> { where(approval_status: "pending") }
scope :approved, -> { where(approval_status: "approved") }
scope :rejected, -> { where(approval_status: "rejected") }
## Wikipedia urls are only necessary when approving a crop
validates :en_wikipedia_url,
format: {
with: %r{\Ahttps?:\/\/en\.wikipedia\.org\/wiki\/[[:alnum:]%_\.()-]+\z},
message: 'is not a valid English Wikipedia URL'
},
if: :approved?
## Reasons are only necessary when rejecting
validates :reason_for_rejection, presence: true, if: :rejected?
## This validation addresses a race condition
validate :approval_status_cannot_be_changed_again
validate :must_be_rejected_if_rejected_reasons_present
validate :must_have_meaningful_reason_for_rejection

home_index.html-----------------------------------------------------------
<div class="row">
<div class="col-md-12">
<% if member_signed_in? %>
<h1>
<%= t('.welcome', site_name: ENV['GROWSTUFF_SITE_NAME'], member_name: cu
rrent_member) %>
</h1>
<%= render :partial => 'stats' %>
<p>
<div class="btn-group">
<%= link_to t('.plant'), new_planting_path, :class => 'btn btn-default
' %>
<%= link_to t('.harvest'), new_harvest_path, :class => 'btn btn-defaul
t' %>
<%= link_to t('.add_seeds'), new_seed_path, :class => 'btn btn-default
' %>
<%= link_to t('.post'), new_post_path, :class => 'btn btn-default' %>
</div>
</p>
<% else %>
<div class="hidden-xs">
<div class="jumbotron">
<%= render :partial => 'blurb' %>
</div>
</div>
<div class="visible-xs">
<%= render :partial => 'blurb' %>
</div>
<% end %>
<%= render :partial => 'crops' %>
<%= render :partial => 'seeds' %>
<%= render :partial => 'members' %>
<%= render :partial => 'discuss' %>
</div>
</div>

account_index.html------------------------------------------
<% content_for :title, "Listing accounts" %>
<table>
<tr>
<th>Member</th>
<th>Account type</th>
<th>Paid until</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @accounts.each do |account| %>
<tr>
<td>
<%= account.member_id %>
</td>
<td>
<%= account.account_type %>
</td>
<td>
<%= account.paid_until %>
</td>
<td>
<%= link_to 'Show', account %>
</td>
<td>
<%= link_to 'Edit', edit_account_path(account) %>
</td>
<td>
<%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are y
ou sure?' } %>
</td>
</tr>
<% end %>
</table>
<br/>
<%= link_to 'New Account detail', new_account_path %>

garden_index.html-------------------------------
<% content_for :title, @owner ? "#{@owner}'s gardens" : "Everyone's gardens" %>
<%= render 'nav', owner: @owner, show_all: @show_all %>
<div class="pagination">
<%= page_entries_info @gardens %>
<%= will_paginate @gardens %>
</div>
<div class="row">
<% if @gardens.size > 0 %>
<% @gardens.each do |garden| %>
<div class="col-md-6">
<%= render :partial => 'gardens/thumbnail', :locals => {:garden => garde
n} %>
</div>
<% end %>
<% else %>
<p>There are no gardens to display.</p>
<% end %>
</div>
<div class="pagination">
<%= page_entries_info @gardens %>
<%= will_paginate @garden %>
</div>
plantings_index.html-------------------------------
<% content_for :title, @owner ? t('.title.owner_plantings', owner: @owner) : @cr
op ? t('.title.crop_plantings', crop: @crop.name) : t('.title.default') %>
<%= render 'nav', owner: @owner, show_all: @show_all %>
<% if @owner %>
<%= link_to "View #{@owner}'s profile >>", member_path(@owner) %>
<% end %>
<div class="pagination">
<%= page_entries_info @plantings %>
<%= will_paginate @plantings %>
</div>
<div class="row">
<% if @plantings.size > 0 %>
<% @plantings.each.with_index do |planting| %>
<div class="col-xs-12 col-lg-6">
<%= render partial: "plantings/thumbnail", locals: {:planting => plantin
g} %>
</div>
<% end %>
<% end %>
</div>
<div class="pagination">
<%= page_entries_info @plantings %>
<%= will_paginate @plantings %>
<ul class="list-inline">
<li>The data on this page is available in the following formats:</li>
<% if @owner %>
<li>
<%= link_to "CSV", plantings_by_owner_path(@owner, :format => 'csv') %>
</li>
<li>
<%= link_to "JSON", plantings_by_owner_path(@owner, :format => 'json') %
>
</li>
<li>
<%= link_to "RSS", plantings_by_owner_path(@owner, :format => 'rss') %>
</li>
<% else %>
<li>
<%= link_to "CSV", plantings_path(:format => 'csv') %>
</li>
<li>
<%= link_to "JSON", plantings_path(:format => 'json') %>
</li>
<li>
<%= link_to "RSS", plantings_path(:format => 'rss') %>
</li>
<% end %>
</ul>
</div>

posts_index.html-----------------------------------------
<% content_for :title, @author ? t('.title.author_posts', author: @author) : t('
.title.default') %>
<p>
<% if can? :create, Post %>
<% if @author %>
<p>
<% if @author == current_member %>
<%= link_to 'Post something', new_post_path, :class => 'btn btn-primar
y' %>
<% end %>
<%= link_to "View everyone's posts", posts_path, :class => 'btn btn-defa
ult' %>
</p>
<% else # everyone's posts %>
<%= link_to 'Post something', new_post_path, :class => 'btn btn-primary' %
>
<% if current_member %>
<%= link_to 'View your posts', posts_by_author_path(:author => current_m
ember.slug), :class => 'btn btn-default' %>
<% end %>
<% end %>
<% else %>
<%= render :partial => 'shared/signin_signup', :locals => { :to => 'write a
post' } %>
<% end %>
</p>
<div class="pagination">
<%= page_entries_info @posts %>
<%= will_paginate @posts %>
</div>
<% unless @posts.empty? %>
<% @posts.each do |post| %>
<%= render :partial => "single", :locals => { :post => post, :subject => tru
e } %>
<% end %>
<div class="pagination">
<%= page_entries_info @posts %>
<%= will_paginate @posts %>
</div>
<% end %>
<p>
<% if @author %>
Subscribe to
<%= succeed "." do %>
<%= link_to "#{@author}'s posts RSS feed", posts_by_author_path(format: 'r
ss', author: @author) %>
<% end %>
<% else %>
Subscribe to the #{ENV['GROWSTUFF_SITE_NAME']}
<%= link_to "posts RSS feed", posts_path(:format => 'rss') %>
or
<%= succeed "." do %>
<%= link_to "comments RSS feed", comments_path(:format => 'rss') %>
<% end %>
<% end %>
</p>

You might also like