mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Admin can list and create producer properties
This commit is contained in:
@@ -7,6 +7,15 @@ module Admin
|
||||
|
||||
helper 'spree/products'
|
||||
|
||||
respond_override update: { html: { success: lambda {
|
||||
if params[:enterprise].key? :producer_properties_attributes
|
||||
redirect_to main_app.admin_enterprise_producer_properties_path(@enterprise)
|
||||
else
|
||||
redirect_to main_app.admin_enterprises_path
|
||||
end
|
||||
} } }
|
||||
|
||||
|
||||
def bulk_update
|
||||
@enterprise_set = EnterpriseSet.new(params[:enterprise_set])
|
||||
if @enterprise_set.save
|
||||
|
||||
20
app/controllers/admin/producer_properties_controller.rb
Normal file
20
app/controllers/admin/producer_properties_controller.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module Admin
|
||||
class ProducerPropertiesController < ResourceController
|
||||
before_filter :find_properties
|
||||
|
||||
before_filter :setup_property, only: [:index]
|
||||
|
||||
|
||||
|
||||
private
|
||||
|
||||
def find_properties
|
||||
@properties = Spree::Property.pluck(:name)
|
||||
end
|
||||
|
||||
def setup_property
|
||||
@enterprise = Enterprise.find params[:enterprise_id]
|
||||
@enterprise.producer_properties.build
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -19,7 +19,7 @@ class Enterprise < ActiveRecord::Base
|
||||
|
||||
delegate :latitude, :longitude, :city, :state_name, :to => :address
|
||||
|
||||
accepts_nested_attributes_for :address
|
||||
accepts_nested_attributes_for :address, :producer_properties
|
||||
has_attached_file :logo, :styles => { :medium => "300x300>", :thumb => "100x100>" }
|
||||
has_attached_file :promo_image, :styles => { :large => "1200x260#", :thumb => "100x100>" }
|
||||
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
class ProducerProperty < ActiveRecord::Base
|
||||
belongs_to :property, class_name: 'Spree::Property'
|
||||
|
||||
def property_name
|
||||
property.name if property
|
||||
end
|
||||
|
||||
def property_name=(name)
|
||||
unless name.blank?
|
||||
self.property = Spree::Property.find_by_name(name) ||
|
||||
Spree::Property.create(name: name, presentation: name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,6 +43,10 @@
|
||||
%br/
|
||||
= link_to_delete_enterprise enterprise
|
||||
%br/
|
||||
- if enterprise.is_primary_producer
|
||||
= link_to_with_icon 'icon-dashboard', 'Properties', main_app.admin_enterprise_producer_properties_path(enterprise_id: enterprise.id)
|
||||
(#{enterprise.producer_properties.count})
|
||||
%br/
|
||||
- if enterprise.is_distributor
|
||||
= link_to_with_icon 'icon-chevron-right', 'Payment Methods', spree.admin_payment_methods_path(enterprise_id: enterprise.id)
|
||||
(#{enterprise.payment_methods.count})
|
||||
@@ -61,4 +65,4 @@
|
||||
- if @enterprises.empty?
|
||||
%tr
|
||||
%td{colspan: "4"}= t(:none)
|
||||
= f.submit 'Update'
|
||||
= f.submit 'Update'
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
%tr.product_property.fields{"data-hook" => "producer_property", id: "#{dom_id(f.object)}"}
|
||||
%td.no-border
|
||||
%span.handle
|
||||
= f.hidden_field :id
|
||||
%td.property_name
|
||||
= f.text_field :property_name, :class => 'autocomplete'
|
||||
%td.value
|
||||
= f.text_field :value, :class => 'autocomplete'
|
||||
%td.actions
|
||||
- unless @enterprise.producer_properties.empty?
|
||||
-#= link_to_remove_fields t(:remove), f, :no_text => true
|
||||
39
app/views/admin/producer_properties/index.html.haml
Normal file
39
app/views/admin/producer_properties/index.html.haml
Normal file
@@ -0,0 +1,39 @@
|
||||
- content_for :page_title do
|
||||
Producer Properties
|
||||
|
||||
|
||||
- content_for :page_actions do
|
||||
%ul.tollbar.inline-menu
|
||||
%li
|
||||
= link_to_add_fields 'Add Producer Property', 'tbody#producer_properties', class: 'icon-plus button'
|
||||
|
||||
|
||||
= render 'spree/shared/error_messages', target: @enterprise
|
||||
|
||||
|
||||
= form_for @enterprise, url: main_app.admin_enterprise_path(@enterprise), method: :put do |f|
|
||||
%fieldset.no-border-top
|
||||
.add_producer_properties{"data-hook" => "add_producer_properties"}
|
||||
= image_tag 'spinner.gif', :plugin => 'spree', :style => 'display:none;', :id => 'busy_indicator'
|
||||
%table.index.sortable{"data-hook" => "", "data-sortable-link" => main_app.update_positions_admin_enterprise_producer_properties_url(@enterprise)}
|
||||
%thead
|
||||
%tr{"data-hook" => "producer_properties_header"}
|
||||
%th{colspan: "2"} Property
|
||||
%th Value
|
||||
%th.actions
|
||||
%tbody#producer_properties{"data-hook" => ""}
|
||||
= f.fields_for :producer_properties do |pp_form|
|
||||
= render 'producer_property_fields', f: pp_form
|
||||
= render 'spree/admin/shared/edit_resource_links', collection_url: main_app.admin_enterprise_producer_properties_path(@enterprise)
|
||||
= hidden_field_tag 'clear_producer_properties', 'true'
|
||||
|
||||
:javascript
|
||||
var properties = #{raw(@properties.to_json)};
|
||||
|
||||
$("#producer_properties input.autocomplete").live("keydown", function() {
|
||||
already_auto_completed = $(this).is('ac_input');
|
||||
if (!already_auto_completed) {
|
||||
$(this).autocomplete({source: properties});
|
||||
$(this).focus();
|
||||
}
|
||||
});
|
||||
@@ -45,6 +45,10 @@ Openfoodnetwork::Application.routes.draw do
|
||||
|
||||
resources :enterprises do
|
||||
post :bulk_update, :on => :collection, :as => :bulk_update
|
||||
|
||||
resources :producer_properties do
|
||||
post :update_positions, on: :collection
|
||||
end
|
||||
end
|
||||
|
||||
resources :enterprise_relationships
|
||||
|
||||
@@ -160,7 +160,33 @@ feature %q{
|
||||
page.should have_selector "a.list-item", text: enterprise_fee.name
|
||||
end
|
||||
|
||||
context 'as an Enterprise user' do
|
||||
describe "producer properties" do
|
||||
it "creates producer properties" do
|
||||
# Given a producer enterprise
|
||||
s = create(:supplier_enterprise)
|
||||
|
||||
# When I go to its properties page
|
||||
login_to_admin_section
|
||||
click_link 'Enterprises'
|
||||
within(".enterprise-#{s.id}") { click_link 'Properties' }
|
||||
|
||||
# And I create a property
|
||||
fill_in 'enterprise_producer_properties_attributes_0_property_name', with: "Certified Organic"
|
||||
fill_in 'enterprise_producer_properties_attributes_0_value', with: "NASAA 12345"
|
||||
click_button 'Update'
|
||||
|
||||
# Then I should be returned to the producer properties page
|
||||
page.should have_selector 'h1.page-title', text: "Producer Properties"
|
||||
|
||||
# And the producer should have the property
|
||||
s.producer_properties(true).count.should == 1
|
||||
s.producer_properties.first.property.presentation.should == "Certified Organic"
|
||||
s.producer_properties.first.value.should == "NASAA 12345"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context "as an Enterprise user" do
|
||||
let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') }
|
||||
let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') }
|
||||
let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') }
|
||||
|
||||
Reference in New Issue
Block a user