diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 7139d3f04..f357e24fb 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -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) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index b6e0acbec..5794a45cc 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -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 diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 234c760f2..3e0e97806 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -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 diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index a9cfebf61..f4d8d45be 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -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 diff --git a/app/models/notification.rb b/app/models/notification.rb index c970d4218..f19de9b34 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index df8609184..85a3c526d 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/spec/helpers/plantings_helper_spec.rb b/spec/helpers/plantings_helper_spec.rb index 9485af81d..e776501b4 100644 --- a/spec/helpers/plantings_helper_spec.rb +++ b/spec/helpers/plantings_helper_spec.rb @@ -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') } diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 89c29785d..1f8bf88bf 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -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