From 0139c4bda2f7d9b44e7a2e1dc9a098a48367dca2 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 13 Jun 2014 11:49:13 +1000 Subject: [PATCH] Set producer property --- app/models/enterprise.rb | 21 ++++++++++++++------- spec/models/enterprise_spec.rb | 19 ++++++++++++++++--- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 132dafe8a0..910851e76f 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -103,13 +103,6 @@ class Enterprise < ActiveRecord::Base end } - - # Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554 - def self.distinct_count - count(distinct: true) - end - - def self.find_near(suburb) enterprises = [] @@ -121,6 +114,20 @@ class Enterprise < ActiveRecord::Base enterprises end + # Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554 + def self.distinct_count + count(distinct: true) + end + + def set_producer_property(property_name, property_value) + transaction do + property = Spree::Property.where(name: property_name).first_or_create!(presentation: property_name) + producer_property = ProducerProperty.where(producer_id: id, property_id: property.id).first_or_initialize + producer_property.value = property_value + producer_property.save! + end + end + def has_supplied_products_on_hand? self.supplied_products.where('count_on_hand > 0').present? end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 542d95a4b0..bdf5574baf 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -449,14 +449,27 @@ describe Enterprise do describe "presentation of attributes" do let(:distributor) { create(:distributor_enterprise, - website: "http://www.google.com", - facebook: "www.facebook.com/roger", - linkedin: "http://linkedin.com") + website: "http://www.google.com", + facebook: "www.facebook.com/roger", + linkedin: "https://linkedin.com") } + it "strips http and www from url fields" do distributor.website.should == "google.com" distributor.facebook.should == "facebook.com/roger" distributor.linkedin.should == "linkedin.com" end end + + describe "producer properties" do + let(:supplier) { create(:supplier_enterprise) } + + it "sets producer properties" do + supplier.set_producer_property 'Organic Certified', 'NASAA 12345' + + supplier.producer_properties.count.should == 1 + supplier.producer_properties.first.value.should == 'NASAA 12345' + supplier.producer_properties.first.property.presentation.should == 'Organic Certified' + end + end end