diff --git a/Gemfile b/Gemfile index dec618a871..742f4bef8a 100644 --- a/Gemfile +++ b/Gemfile @@ -50,7 +50,7 @@ group :assets do gem 'turbo-sprockets-rails3' gem 'foundation-icons-sass-rails' - + gem 'momentjs-rails' end gem "foundation-rails" gem 'foundation_rails_helper', github: 'willrjmarshall/foundation_rails_helper', branch: "rails3" diff --git a/Gemfile.lock b/Gemfile.lock index 971e536da5..82f4d97ba8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -327,6 +327,8 @@ GEM method_source (0.8.1) mime-types (1.25.1) mini_portile (0.5.2) + momentjs-rails (2.5.1) + railties (>= 3.1) money (5.0.0) i18n (~> 0.4) json @@ -527,6 +529,7 @@ DEPENDENCIES json_spec letter_opener local_organics_feature! + momentjs-rails newrelic_rpm oj paperclip diff --git a/app/assets/javascripts/darkswarm/all.js.coffee b/app/assets/javascripts/darkswarm/all.js.coffee index 6783a6f9e2..86eaa3b2c7 100644 --- a/app/assets/javascripts/darkswarm/all.js.coffee +++ b/app/assets/javascripts/darkswarm/all.js.coffee @@ -13,8 +13,8 @@ #= require ../search/jquery.backstretch.js #= require angular-backstretch.js #= require angular-flash.min.js +#= require moment # -#= require ../shared/jquery.timeago #= require foundation #= require ./darkswarm #= require ./overrides diff --git a/app/assets/javascripts/darkswarm/darkswarm.js.coffee b/app/assets/javascripts/darkswarm/darkswarm.js.coffee index 2bd874402c..1af2f22ad2 100644 --- a/app/assets/javascripts/darkswarm/darkswarm.js.coffee +++ b/app/assets/javascripts/darkswarm/darkswarm.js.coffee @@ -1,4 +1,4 @@ -window.Darkswarm = angular.module("Darkswarm", ["ngResource", "filters", 'mm.foundation', 'angularLocalStorage', 'pasvaz.bindonce', 'infinite-scroll', 'angular-flash.service', 'backstretch']).config ($httpProvider, $tooltipProvider) -> +window.Darkswarm = angular.module("Darkswarm", ["ngResource", 'mm.foundation', 'angularLocalStorage', 'pasvaz.bindonce', 'infinite-scroll', 'angular-flash.service', 'backstretch']).config ($httpProvider, $tooltipProvider) -> $httpProvider.defaults.headers.post['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content') $httpProvider.defaults.headers.put['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content') $httpProvider.defaults.headers['common']['X-Requested-With'] = 'XMLHttpRequest' diff --git a/app/assets/javascripts/darkswarm/filters/dates.js.coffee b/app/assets/javascripts/darkswarm/filters/dates.js.coffee new file mode 100644 index 0000000000..9091c80c7c --- /dev/null +++ b/app/assets/javascripts/darkswarm/filters/dates.js.coffee @@ -0,0 +1,10 @@ +Darkswarm.filter "date_in_words", -> + (date) -> + moment(date).fromNow() + +Darkswarm.filter "sensible_timeframe", (date_in_wordsFilter)-> + (date) -> + if moment().add('days', 2) < moment(date) + "Orders open" + else + "Closing in #{date_in_wordsFilter(date)}" diff --git a/app/assets/javascripts/darkswarm/shop.js.coffee b/app/assets/javascripts/darkswarm/filters/truncate.js.coffee similarity index 60% rename from app/assets/javascripts/darkswarm/shop.js.coffee rename to app/assets/javascripts/darkswarm/filters/truncate.js.coffee index 70917a934d..0f05cbc1b4 100644 --- a/app/assets/javascripts/darkswarm/shop.js.coffee +++ b/app/assets/javascripts/darkswarm/filters/truncate.js.coffee @@ -1,4 +1,4 @@ -angular.module("filters", []).filter "truncate", -> +Darkswarm.filter "truncate", -> (text, length, end) -> text = text || "" length = 10 if isNaN(length) @@ -7,8 +7,3 @@ angular.module("filters", []).filter "truncate", -> text else String(text).substring(0, length - end.length) + end - -$.timeago.settings.allowFuture = true; -angular.module("filters").filter "date_in_words", -> - (date) -> - $.timeago(date) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 317c092375..96154c5a9b 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -64,6 +64,11 @@ class OrderCycle < ActiveRecord::Base with_distributor(distributor).soonest_opening.first end + def self.first_closing_for(distributor) + with_distributor(distributor).soonest_closing.first + end + + def self.most_recently_closed_for(distributor) with_distributor(distributor).most_recently_closed.first end diff --git a/app/views/darkswarm/_skinny.html.haml b/app/views/darkswarm/_skinny.html.haml index 4431a12560..fcf860575e 100644 --- a/app/views/darkswarm/_skinny.html.haml +++ b/app/views/darkswarm/_skinny.html.haml @@ -6,7 +6,7 @@ .columns.small-1 {{ hub.address.state | uppercase }} .columns.small-3{"bo-if" => "hub.active"} - Orders closing when? + {{ hub.orders_close_at | sensible_timeframe }} .columns.small-3{"bo-if" => "!hub.active"} Orders closed .columns.small-1.text-right diff --git a/app/views/darkswarm/index.html.haml b/app/views/darkswarm/index.html.haml index 6c9261fa0a..17faff2d1f 100644 --- a/app/views/darkswarm/index.html.haml +++ b/app/views/darkswarm/index.html.haml @@ -37,8 +37,6 @@ .small-12.columns = render partial: 'darkswarm/skinny' = render partial: 'darkswarm/fat' - - .small-12.columns .row{"ng-show" => "filteredHubs.length == 0"} .columns.small-12.text-center No results diff --git a/app/views/json/_hubs.rabl b/app/views/json/_hubs.rabl index 062df2b267..a4acede07c 100644 --- a/app/views/json/_hubs.rabl +++ b/app/views/json/_hubs.rabl @@ -25,5 +25,5 @@ node :active do |hub| end node :orders_close_at do |hub| - OrderCycle.with_distributor(hub).soonest_closing.first.andand.orders_close_at + OrderCycle.first_closing_for(hub).andand.orders_close_at end diff --git a/spec/controllers/darkswarm_controller_spec.rb b/spec/controllers/darkswarm_controller_spec.rb index a1dc1dd23a..975ffa5995 100644 --- a/spec/controllers/darkswarm_controller_spec.rb +++ b/spec/controllers/darkswarm_controller_spec.rb @@ -14,10 +14,9 @@ describe DarkswarmController do assigns[:active_distributors].should == [distributor] end - # This is done inside RABL template + # This is done inside the json/hubs RABL template it "gets the next order cycle for each hub" do - OrderCycle.stub_chain(:with_distributor, :soonest_closing, :first) - OrderCycle.should_receive(:with_distributor).with(distributor) + OrderCycle.should_receive(:first_closing_for).with(distributor) get :index end end diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index cb2da1e0b4..e4f979dc5b 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -494,4 +494,13 @@ describe OrderCycle do OrderCycle.first_opening_for(distributor).should == nil end end + + describe "finding open order cycles" do + it "should give the soonest closing order cycle for a distributor" do + distributor = create(:distributor_enterprise) + oc = create(:simple_order_cycle, name: 'oc 1', distributors: [distributor], orders_open_at: 1.days.ago, orders_close_at: 11.days.from_now) + oc2 = create(:simple_order_cycle, name: 'oc 2', distributors: [distributor], orders_open_at: 2.days.ago, orders_close_at: 12.days.from_now) + OrderCycle.first_closing_for(distributor).should == oc + end + end end