Merge remote-tracking branch 'origin/photo_concern' into photo_concern

This commit is contained in:
Mackenzie Morgan
2016-12-01 00:19:42 -05:00
8 changed files with 94 additions and 14 deletions

View File

@@ -9,15 +9,12 @@ class CropsController < ApplicationController
# GET /crops.json
def index
@sort = params[:sort]
if @sort == 'alpha'
# alphabetical order
@crops = Crop.includes(:scientific_names, { plantings: :photos })
@paginated_crops = @crops.approved.paginate(page: params[:page])
else
# default to sorting by popularity
@crops = Crop.popular.includes(:scientific_names, { plantings: :photos })
@paginated_crops = @crops.approved.paginate(page: params[:page])
end
@crops = if @sort == 'alpha'
Crop.includes(:scientific_names, { plantings: :photos })
else
popular_crops
end
@paginated_crops = @crops.approved.paginate(page: params[:page])
respond_to do |format|
format.html
@@ -198,6 +195,10 @@ class CropsController < ApplicationController
private
def popular_crops
Crop.popular.includes(:scientific_names, { plantings: :photos })
end
def recreate_names(param_name, name_type)
return unless params[param_name].present?
destroy_names(name_type)

View File

@@ -5,7 +5,7 @@ class NotificationsController < ApplicationController
# GET /notifications
def index
@notifications = Notification.where(recipient_id: current_member).page(params[:page])
@notifications = Notification.by_recipient(current_member).page(params[:page])
respond_to do |format|
format.html # index.html.erb

View File

@@ -4,7 +4,7 @@ class OrdersController < ApplicationController
# GET /orders
def index
@orders = Order.where(member_id: current_member.id)
@orders = Order.by_member(current_member)
respond_to do |format|
format.html # index.html.erb

View File

@@ -1,13 +1,13 @@
module PlantingsHelper
def display_days_before_maturity(planting)
if planting.finished?
0
"0"
elsif !planting.finished_at.nil?
((p = planting.finished_at - DateTime.now).to_i) <= 0 ? 0 : p.to_i
((p = planting.finished_at - Date.current).to_i) <= 0 ? "0" : p.to_i.to_s
elsif planting.planted_at.nil? || planting.days_before_maturity.nil?
"unknown"
else
((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i <= 0) ? 0 : p.to_i
((p = (planting.planted_at + planting.days_before_maturity) - Date.current).to_i <= 0) ? "0" : p.to_i.to_s
end
end

View File

@@ -7,6 +7,7 @@ class Notification < ActiveRecord::Base
default_scope { order('created_at DESC') }
scope :unread, -> { where(read: false) }
scope :by_recipient, ->(recipient) { where(recipient_id: recipient) }
before_create :replace_blank_subject
after_create :send_email

View File

@@ -12,6 +12,8 @@ class Order < ActiveRecord::Base
before_save :standardize_referral_code
scope :by_member, ->(member) { where(member: member) }
# total price of an order
def total
sum = 0

View File

@@ -1,6 +1,69 @@
require 'rails_helper'
describe PlantingsHelper do
describe "display_days_before_maturity" do
it "handles nil planted_at, nil finished_at, non-nil days_until_maturity" do
planting = FactoryGirl.build(:planting,
quantity: 5,
planted_at: nil,
finished_at: nil,
days_before_maturity: 17
)
result = helper.display_days_before_maturity(planting)
expect(result).to eq "unknown"
end
it "handles non-nil planted_at and d_b_m, nil finished_at" do
planting = FactoryGirl.build(:planting,
quantity: 5,
planted_at: Date.current,
finished_at: nil,
days_before_maturity: 17
)
result = helper.display_days_before_maturity(planting)
expect(result).to eq "17"
end
it "handles completed plantings" do
planting = FactoryGirl.build(:planting, finished: true)
result = helper.display_days_before_maturity(planting)
expect(result).to eq "0"
end
it "handles plantings that should have finished" do
planting = FactoryGirl.build(:planting,
quantity: 5,
planted_at: Date.new(0, 1, 1),
finished_at: nil,
days_before_maturity: "17"
)
result = helper.display_days_before_maturity(planting)
expect(result).to eq "0"
end
it "handles nil d_b_m and nil finished_at" do
planting = FactoryGirl.build(:planting,
quantity: 5,
planted_at: Date.new(2012, 5, 12),
finished_at: nil,
days_before_maturity: nil
)
result = helper.display_days_before_maturity(planting)
expect(result).to eq "unknown"
end
it "handles finished_at dates in the future" do
planting = FactoryGirl.build(:planting,
quantity: 5,
planted_at: Date.current,
finished_at: Date.current + 5,
days_before_maturity: nil
)
result = helper.display_days_before_maturity(planting)
expect(result).to eq "5"
end
end
describe "display_planting" do
let!(:member) { FactoryGirl.build(:member, login_name: 'crop_lady') }

View File

@@ -8,6 +8,19 @@ describe Order do
order_id: @order.id, product_id: @product.id)
end
describe '#by_member_id' do
before do
@member1 = FactoryGirl.create(:member)
@member2 = FactoryGirl.create(:member)
@order1 = Order.create!(member_id: @member1.id)
@order2 = Order.create!(member_id: @member2.id)
end
it "only returns orders belonging to member" do
Order.by_member(@member1).should eq [@order1]
end
end
it 'has order_items' do
@order.order_items.first.should eq @order_item
end