mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Making TagRule autocomplete available to models other than Customer
Incorporate ng admin.tags module and rails TagController into existing admin.tagRules module + TagRuleController
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
angular.module("admin.customers").controller "customersCtrl", ($scope, CustomerResource, TagsResource, $q, Columns, pendingChanges, shops) ->
|
||||
angular.module("admin.customers").controller "customersCtrl", ($scope, CustomerResource, TagRuleResource, $q, Columns, pendingChanges, shops) ->
|
||||
$scope.shop = {}
|
||||
$scope.shops = shops
|
||||
$scope.submitAll = pendingChanges.submitAll
|
||||
@@ -16,7 +16,7 @@ angular.module("admin.customers").controller "customersCtrl", ($scope, CustomerR
|
||||
defer = $q.defer()
|
||||
params =
|
||||
enterprise_id: $scope.shop.id
|
||||
TagsResource.index params, (data) =>
|
||||
TagRuleResource.mapByTag params, (data) =>
|
||||
filtered = data.filter (tag) ->
|
||||
tag.text.toLowerCase().indexOf(query.toLowerCase()) != -1
|
||||
defer.resolve filtered
|
||||
|
||||
@@ -1 +1 @@
|
||||
angular.module("admin.customers", ['ngResource', 'ngTagsInput', 'admin.indexUtils', 'admin.utils', 'admin.dropdown'])
|
||||
angular.module("admin.customers", ['ngResource', 'admin.tagRules', 'admin.indexUtils', 'admin.utils', 'admin.dropdown'])
|
||||
@@ -1,9 +0,0 @@
|
||||
angular.module("admin.customers").factory 'TagsResource', ($resource) ->
|
||||
$resource('/admin/tags.json', {}, {
|
||||
'index':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
cache: true
|
||||
params:
|
||||
enterprise_id: '@enterprise_id'
|
||||
})
|
||||
@@ -1,2 +1,10 @@
|
||||
angular.module("admin.shippingMethods").controller "shippingMethodCtrl", ($scope, shippingMethod) ->
|
||||
angular.module("admin.shippingMethods").controller "shippingMethodCtrl", ($scope, shippingMethod, TagRuleResource, $q) ->
|
||||
$scope.shippingMethod = shippingMethod
|
||||
|
||||
$scope.findTags = (query) ->
|
||||
defer = $q.defer()
|
||||
TagRuleResource.mapByTag (data) =>
|
||||
filtered = data.filter (tag) ->
|
||||
tag.text.toLowerCase().indexOf(query.toLowerCase()) != -1
|
||||
defer.resolve filtered
|
||||
defer.promise
|
||||
|
||||
@@ -1 +1 @@
|
||||
angular.module("admin.shippingMethods", ["ngTagsInput", 'admin.utils', 'templates'])
|
||||
angular.module("admin.shippingMethods", ["ngTagsInput", 'admin.utils', 'admin.tagRules', 'templates'])
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
angular.module("admin.tagRules").factory 'TagRuleResource', ($resource) ->
|
||||
$resource('/admin/tag_rules/:action.json', {}, {
|
||||
'mapByTag':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
cache: true
|
||||
params:
|
||||
action: 'map_by_tag'
|
||||
enterprise_id: '@enterprise_id'
|
||||
})
|
||||
@@ -1 +1 @@
|
||||
angular.module("admin.tagRules", ['ngTagsInput'])
|
||||
angular.module("admin.tagRules", ['ngResource', 'ngTagsInput'])
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -7,11 +7,8 @@ module Admin
|
||||
respond_to do |format|
|
||||
format.html
|
||||
format.json do
|
||||
serialised = ActiveModel::ArraySerializer.new(
|
||||
@collection,
|
||||
each_serializer: Api::Admin::CustomerSerializer,
|
||||
spree_current_user: spree_current_user)
|
||||
render json: serialised.to_json
|
||||
tag_rule_mapping = TagRule.mapping_for(Enterprise.where(id: params[:enterprise_id]))
|
||||
render_as_json @collection, tag_rule_mapping: tag_rule_mapping
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -20,7 +17,8 @@ module Admin
|
||||
@customer = Customer.new(params[:customer])
|
||||
if user_can_create_customer?
|
||||
@customer.save
|
||||
render json: Api::Admin::CustomerSerializer.new(@customer).to_json
|
||||
tag_rule_mapping = TagRule.mapping_for(Enterprise.where(id: @customer.enterprise))
|
||||
render_as_json @customer, tag_rule_mapping: tag_rule_mapping
|
||||
else
|
||||
redirect_to '/unauthorized'
|
||||
end
|
||||
|
||||
@@ -6,5 +6,38 @@ module Admin
|
||||
respond_override destroy: { json: {
|
||||
success: lambda { render nothing: true, :status => 204 }
|
||||
} }
|
||||
|
||||
def map_by_tag
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
serialiser = ActiveModel::ArraySerializer.new(collection)
|
||||
render json: serialiser.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def collection_actions
|
||||
[:index, :map_by_tag]
|
||||
end
|
||||
|
||||
def collection
|
||||
case action
|
||||
when :map_by_tag
|
||||
TagRule.mapping_for(enterprises).values
|
||||
else
|
||||
TagRule.for(enterprises.pluck(&:id))
|
||||
end
|
||||
end
|
||||
|
||||
def enterprises
|
||||
if params[:enterprise_id]
|
||||
Enterprise.managed_by(spree_current_user).where(id: params[:enterprise_id])
|
||||
else
|
||||
Enterprise.managed_by(spree_current_user)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
module Admin
|
||||
class TagsController < Spree::Admin::BaseController
|
||||
respond_to :json
|
||||
|
||||
def index
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
serialiser = ActiveModel::ArraySerializer.new(tags_of_enterprise)
|
||||
render json: serialiser.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def enterprise
|
||||
Enterprise.managed_by(spree_current_user).find_by_id(params[:enterprise_id])
|
||||
end
|
||||
|
||||
def tags_of_enterprise
|
||||
return [] unless enterprise
|
||||
tag_rule_map = enterprise.rules_per_tag
|
||||
tag_rule_map.keys.map do |tag|
|
||||
{ text: tag, rules: tag_rule_map[tag] }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -351,20 +351,6 @@ class Enterprise < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def rules_per_tag
|
||||
tag_rule_map = {}
|
||||
tag_rules.each do |rule|
|
||||
rule.preferred_customer_tags.split(",").each do |tag|
|
||||
if tag_rule_map[tag]
|
||||
tag_rule_map[tag] += 1
|
||||
else
|
||||
tag_rule_map[tag] = 1
|
||||
end
|
||||
end
|
||||
end
|
||||
tag_rule_map
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def devise_mailer
|
||||
|
||||
@@ -72,7 +72,7 @@ class AbilityDecorator
|
||||
|
||||
can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], ProducerProperty
|
||||
|
||||
can [:admin, :destroy], TagRule do |tag_rule|
|
||||
can [:admin, :map_by_tag, :destroy], TagRule do |tag_rule|
|
||||
user.enterprises.include? tag_rule.enterprise
|
||||
end
|
||||
|
||||
@@ -218,7 +218,6 @@ class AbilityDecorator
|
||||
|
||||
can [:create], Customer
|
||||
can [:admin, :index, :update, :destroy], Customer, enterprise_id: Enterprise.managed_by(user).pluck(:id)
|
||||
can [:admin, :index], :tag
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ class TagRule < ActiveRecord::Base
|
||||
|
||||
attr_accessible :enterprise, :enterprise_id, :preferred_customer_tags
|
||||
|
||||
scope :for, lambda { |enterprises| where(enterprise_id: enterprises) }
|
||||
|
||||
def set_context(subject, context)
|
||||
@subject = subject
|
||||
@context = context
|
||||
@@ -24,6 +26,19 @@ class TagRule < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def self.mapping_for(enterprises)
|
||||
self.for(enterprises).inject({}) do |mapping, rule|
|
||||
rule.preferred_customer_tags.split(",").each do |tag|
|
||||
if mapping[tag]
|
||||
mapping[tag][:rules] += 1
|
||||
else
|
||||
mapping[tag] = { text: tag, rules: 1 }
|
||||
end
|
||||
end
|
||||
mapping
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def relevant?
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
= f.label :tags, t(:tags)
|
||||
.omega.eight.columns
|
||||
= f.hidden_field :tag_list, "ng-value" => "shippingMethod.tag_list"
|
||||
%tags-with-translation#something{ object: "shippingMethod" }
|
||||
%tags-with-translation#something{ object: "shippingMethod", 'find-tags' => 'findTags(query)' }
|
||||
|
||||
.row
|
||||
.alpha.eleven.columns
|
||||
|
||||
@@ -6,10 +6,9 @@ class Api::Admin::CustomerSerializer < ActiveModel::Serializer
|
||||
end
|
||||
|
||||
def tags
|
||||
tag_rule_map = object.enterprise.rules_per_tag
|
||||
object.tag_list.map do |tag|
|
||||
{ text: tag, rules: tag_rule_map[tag] }
|
||||
tag_rule_map = options[:tag_rule_mapping][tag]
|
||||
tag_rule_map || { text: tag, rules: nil }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -117,7 +117,9 @@ Openfoodnetwork::Application.routes.draw do
|
||||
|
||||
resources :customers, only: [:index, :create, :update, :destroy]
|
||||
|
||||
resources :tags, only: [:index], format: :json
|
||||
resources :tag_rules, only: [], format: :json do
|
||||
get :map_by_tag, on: :collection
|
||||
end
|
||||
|
||||
resource :content
|
||||
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
//= require angular-flash.min.js
|
||||
//= require shared/ng-tags-input.min.js
|
||||
//= require shared/mm-foundation-tpls-0.8.0.min.js
|
||||
//= require textAngular.min.js
|
||||
//= require textAngular-rangy.min.js
|
||||
//= require textAngular-sanitize.min.js
|
||||
//= require textAngular.min.js
|
||||
//= require moment
|
||||
|
||||
angular.module('templates', [])
|
||||
|
||||
@@ -56,7 +56,7 @@ describe "CustomersCtrl", ->
|
||||
{ text: 'three' }
|
||||
]
|
||||
beforeEach ->
|
||||
http.expectGET('/admin/tags.json?enterprise_id=1').respond 200, tags
|
||||
http.expectGET('/admin/tag_rules/map_by_tag.json?enterprise_id=1').respond 200, tags
|
||||
|
||||
it "retrieves the tag list", ->
|
||||
promise = scope.findTags('')
|
||||
|
||||
@@ -3,7 +3,8 @@ describe Api::Admin::CustomerSerializer do
|
||||
let!(:tag_rule) { create(:tag_rule, enterprise: customer.enterprise, preferred_customer_tags: "two") }
|
||||
|
||||
it "serializes a customer" do
|
||||
serializer = Api::Admin::CustomerSerializer.new customer
|
||||
tag_rule_mapping = TagRule.mapping_for(Enterprise.where(id: customer.enterprise_id))
|
||||
serializer = Api::Admin::CustomerSerializer.new customer, tag_rule_mapping: tag_rule_mapping
|
||||
result = JSON.parse(serializer.to_json)
|
||||
expect(result['email']).to eq customer.email
|
||||
tags = result['tags']
|
||||
|
||||
Reference in New Issue
Block a user