Merge pull request #6203 from luisramos0/rubocop_fix

Fix ~80 rubocop issues in serializers/api/admin
This commit is contained in:
Luis Ramos
2020-10-30 16:48:22 +00:00
committed by GitHub
43 changed files with 867 additions and 706 deletions

View File

@@ -556,40 +556,6 @@ Style/ClassAndModuleChildren:
- 'app/models/tag_rule/filter_order_cycles.rb'
- 'app/models/tag_rule/filter_payment_methods.rb'
- 'app/models/tag_rule/filter_shipping_methods.rb'
- 'app/serializers/api/address_serializer.rb'
- 'app/serializers/api/admin/basic_enterprise_fee_serializer.rb'
- 'app/serializers/api/admin/basic_enterprise_serializer.rb'
- 'app/serializers/api/admin/basic_order_cycle_serializer.rb'
- 'app/serializers/api/admin/calculator/flat_percent_item_total_serializer.rb'
- 'app/serializers/api/admin/calculator_serializer.rb'
- 'app/serializers/api/admin/column_preference_serializer.rb'
- 'app/serializers/api/admin/enterprise_fee_serializer.rb'
- 'app/serializers/api/admin/enterprise_relationship_permission_serializer.rb'
- 'app/serializers/api/admin/enterprise_relationship_serializer.rb'
- 'app/serializers/api/admin/enterprise_role_serializer.rb'
- 'app/serializers/api/admin/enterprise_serializer.rb'
- 'app/serializers/api/admin/exchange_serializer.rb'
- 'app/serializers/api/admin/for_order_cycle/enterprise_serializer.rb'
- 'app/serializers/api/admin/for_order_cycle/supplied_product_serializer.rb'
- 'app/serializers/api/admin/id_name_serializer.rb'
- 'app/serializers/api/admin/id_serializer.rb'
- 'app/serializers/api/admin/index_enterprise_serializer.rb'
- 'app/serializers/api/admin/inventory_item_serializer.rb'
- 'app/serializers/api/admin/line_item_serializer.rb'
- 'app/serializers/api/admin/order_cycle_serializer.rb'
- 'app/serializers/api/admin/order_serializer.rb'
- 'app/serializers/api/admin/payment_method/base_serializer.rb'
- 'app/serializers/api/admin/payment_method/stripe_serializer.rb'
- 'app/serializers/api/admin/product_serializer.rb'
- 'app/serializers/api/admin/shipping_method_serializer.rb'
- 'app/serializers/api/admin/tag_rule_serializer.rb'
- 'app/serializers/api/admin/tax_category_serializer.rb'
- 'app/serializers/api/admin/taxon_serializer.rb'
- 'app/serializers/api/admin/units_product_serializer.rb'
- 'app/serializers/api/admin/units_variant_serializer.rb'
- 'app/serializers/api/admin/user_serializer.rb'
- 'app/serializers/api/admin/variant_override_serializer.rb'
- 'app/serializers/api/admin/variant_serializer.rb'
- 'app/serializers/api/country_serializer.rb'
- 'app/serializers/api/currency_config_serializer.rb'
- 'app/serializers/api/current_order_serializer.rb'
@@ -844,47 +810,6 @@ Style/FrozenStringLiteralComment:
- 'app/models/variant_override_set.rb'
- 'app/serializers/api/address_serializer.rb'
- 'app/serializers/api/adjustment_serializer.rb'
- 'app/serializers/api/admin/basic_enterprise_fee_serializer.rb'
- 'app/serializers/api/admin/basic_enterprise_serializer.rb'
- 'app/serializers/api/admin/basic_order_cycle_serializer.rb'
- 'app/serializers/api/admin/calculator/flat_percent_item_total_serializer.rb'
- 'app/serializers/api/admin/calculator_serializer.rb'
- 'app/serializers/api/admin/column_preference_serializer.rb'
- 'app/serializers/api/admin/enterprise_fee_serializer.rb'
- 'app/serializers/api/admin/enterprise_relationship_permission_serializer.rb'
- 'app/serializers/api/admin/enterprise_relationship_serializer.rb'
- 'app/serializers/api/admin/enterprise_role_serializer.rb'
- 'app/serializers/api/admin/enterprise_serializer.rb'
- 'app/serializers/api/admin/exchange_serializer.rb'
- 'app/serializers/api/admin/for_order_cycle/enterprise_serializer.rb'
- 'app/serializers/api/admin/for_order_cycle/supplied_product_serializer.rb'
- 'app/serializers/api/admin/id_email_serializer.rb'
- 'app/serializers/api/admin/id_name_serializer.rb'
- 'app/serializers/api/admin/id_serializer.rb'
- 'app/serializers/api/admin/index_enterprise_serializer.rb'
- 'app/serializers/api/admin/index_order_cycle_serializer.rb'
- 'app/serializers/api/admin/inventory_item_serializer.rb'
- 'app/serializers/api/admin/line_item_serializer.rb'
- 'app/serializers/api/admin/order_cycle_serializer.rb'
- 'app/serializers/api/admin/order_serializer.rb'
- 'app/serializers/api/admin/payment_method/base_serializer.rb'
- 'app/serializers/api/admin/payment_method/stripe_serializer.rb'
- 'app/serializers/api/admin/payment_method_serializer.rb'
- 'app/serializers/api/admin/product_serializer.rb'
- 'app/serializers/api/admin/proxy_order_serializer.rb'
- 'app/serializers/api/admin/schedule_serializer.rb'
- 'app/serializers/api/admin/shipping_method_serializer.rb'
- 'app/serializers/api/admin/subscription_customer_serializer.rb'
- 'app/serializers/api/admin/subscription_line_item_serializer.rb'
- 'app/serializers/api/admin/subscription_serializer.rb'
- 'app/serializers/api/admin/tag_rule_serializer.rb'
- 'app/serializers/api/admin/tax_category_serializer.rb'
- 'app/serializers/api/admin/taxon_serializer.rb'
- 'app/serializers/api/admin/units_product_serializer.rb'
- 'app/serializers/api/admin/units_variant_serializer.rb'
- 'app/serializers/api/admin/user_serializer.rb'
- 'app/serializers/api/admin/variant_override_serializer.rb'
- 'app/serializers/api/admin/variant_serializer.rb'
- 'app/serializers/api/cached_enterprise_serializer.rb'
- 'app/serializers/api/country_serializer.rb'
- 'app/serializers/api/credit_card_serializer.rb'

View File

@@ -1,3 +1,9 @@
class Api::Admin::BasicEnterpriseFeeSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id
# frozen_string_literal: true
module Api
module Admin
class BasicEnterpriseFeeSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id
end
end
end

View File

@@ -1,12 +1,18 @@
class Api::Admin::BasicEnterpriseSerializer < ActiveModel::Serializer
attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category,
:payment_method_ids, :shipping_method_ids, :producer_profile_only, :permalink
# frozen_string_literal: true
def payment_method_ids
object.payment_methods.map(&:id)
end
module Api
module Admin
class BasicEnterpriseSerializer < ActiveModel::Serializer
attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category,
:payment_method_ids, :shipping_method_ids, :producer_profile_only, :permalink
def shipping_method_ids
object.shipping_methods.map(&:id)
def payment_method_ids
object.payment_methods.map(&:id)
end
def shipping_method_ids
object.shipping_methods.map(&:id)
end
end
end
end

View File

@@ -1,20 +1,26 @@
class Api::Admin::BasicOrderCycleSerializer < ActiveModel::Serializer
include OrderCyclesHelper
# frozen_string_literal: true
attributes :id, :name, :status, :orders_open_at, :orders_close_at
module Api
module Admin
class BasicOrderCycleSerializer < ActiveModel::Serializer
include OrderCyclesHelper
has_many :suppliers, serializer: Api::Admin::IdNameSerializer
has_many :distributors, serializer: Api::Admin::IdNameSerializer
attributes :id, :name, :status, :orders_open_at, :orders_close_at
def status
order_cycle_status_class object
end
has_many :suppliers, serializer: Api::Admin::IdNameSerializer
has_many :distributors, serializer: Api::Admin::IdNameSerializer
def orders_open_at
object.orders_open_at.andand.strftime("%F %T %z")
end
def status
order_cycle_status_class object
end
def orders_close_at
object.orders_close_at.andand.strftime("%F %T %z")
def orders_open_at
object.orders_open_at.andand.strftime("%F %T %z")
end
def orders_close_at
object.orders_close_at.andand.strftime("%F %T %z")
end
end
end
end

View File

@@ -1,7 +1,15 @@
class Api::Admin::Calculator::FlatPercentItemTotalSerializer < ActiveModel::Serializer
attributes :id, :preferred_flat_percent
# frozen_string_literal: true
def preferred_flat_percent
object.preferred_flat_percent.to_i
module Api
module Admin
module Calculator
class FlatPercentItemTotalSerializer < ActiveModel::Serializer
attributes :id, :preferred_flat_percent
def preferred_flat_percent
object.preferred_flat_percent.to_i
end
end
end
end
end

View File

@@ -1,7 +1,13 @@
class Api::Admin::CalculatorSerializer < ActiveModel::Serializer
attributes :name, :description
# frozen_string_literal: true
delegate :name, to: :object
module Api
module Admin
class CalculatorSerializer < ActiveModel::Serializer
attributes :name, :description
delegate :description, to: :object
delegate :name, to: :object
delegate :description, to: :object
end
end
end

View File

@@ -1,3 +1,9 @@
class Api::Admin::ColumnPreferenceSerializer < ActiveModel::Serializer
attributes :id, :user_id, :action_name, :column_name, :name, :visible
# frozen_string_literal: true
module Api
module Admin
class ColumnPreferenceSerializer < ActiveModel::Serializer
attributes :id, :user_id, :action_name, :column_name, :name, :visible
end
end
end

View File

@@ -1,57 +1,61 @@
# frozen_string_literal: true
class Api::Admin::CustomerSerializer < ActiveModel::Serializer
attributes :id, :email, :enterprise_id, :user_id, :code, :tags, :tag_list, :name,
:allow_charges, :default_card_present?, :balance, :balance_status
module Api
module Admin
class CustomerSerializer < ActiveModel::Serializer
attributes :id, :email, :enterprise_id, :user_id, :code, :tags, :tag_list, :name,
:allow_charges, :default_card_present?, :balance, :balance_status
has_one :ship_address, serializer: Api::AddressSerializer
has_one :bill_address, serializer: Api::AddressSerializer
has_one :ship_address, serializer: Api::AddressSerializer
has_one :bill_address, serializer: Api::AddressSerializer
def tag_list
customer_tag_list.join(",")
end
def name
object.name.presence || object.bill_address.andand.full_name
end
def name
object.name.presence || object.bill_address.andand.full_name
end
def tag_list
customer_tag_list.join(",")
end
def balance
Spree::Money.new(balance_value, currency: Spree::Config[:currency]).to_s
end
def balance
Spree::Money.new(balance_value, currency: Spree::Config[:currency]).to_s
end
def balance_status
if balance_value.positive?
"credit_owed"
elsif balance_value.negative?
"balance_due"
else
""
def balance_status
if balance_value.positive?
"credit_owed"
elsif balance_value.negative?
"balance_due"
else
""
end
end
def tags
customer_tag_list.map do |tag|
tag_rule_map = options[:tag_rule_mapping].andand[tag]
tag_rule_map || { text: tag, rules: nil }
end
end
def default_card_present?
return unless object.user
object.user.default_card.present?
end
private
def customer_tag_list
return object.tag_list unless options[:customer_tags]
options[:customer_tags].andand[object.id] || []
end
def balance_value
@balance_value ||=
OpenFoodNetwork::UserBalanceCalculator.new(object.email, object.enterprise).balance
end
end
end
def tags
customer_tag_list.map do |tag|
tag_rule_map = options[:tag_rule_mapping].andand[tag]
tag_rule_map || { text: tag, rules: nil }
end
end
def default_card_present?
return unless object.user
object.user.default_card.present?
end
private
def customer_tag_list
return object.tag_list unless options[:customer_tags]
options[:customer_tags].andand[object.id] || []
end
def balance_value
@balance_value ||=
OpenFoodNetwork::UserBalanceCalculator.new(object.email, object.enterprise).balance
end
end

View File

@@ -1,26 +1,32 @@
class Api::Admin::EnterpriseFeeSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :fee_type, :name, :tax_category_id, :inherits_tax_category,
:calculator_type, :enterprise_name, :calculator_description, :calculator_settings
# frozen_string_literal: true
def enterprise_name
object.enterprise.andand.name
end
module Api
module Admin
class EnterpriseFeeSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :fee_type, :name, :tax_category_id, :inherits_tax_category,
:calculator_type, :enterprise_name, :calculator_description, :calculator_settings
def calculator_description
object.calculator.andand.description
end
def enterprise_name
object.enterprise.andand.name
end
def calculator_settings
return nil unless options[:include_calculators]
def calculator_description
object.calculator.andand.description
end
result = nil
def calculator_settings
return nil unless options[:include_calculators]
options[:controller].__send__(:with_format, :html) do
result = options[:controller].
render_to_string(partial: 'admin/enterprise_fees/calculator_settings',
locals: { enterprise_fee: object })
result = nil
options[:controller].__send__(:with_format, :html) do
result = options[:controller].
render_to_string(partial: 'admin/enterprise_fees/calculator_settings',
locals: { enterprise_fee: object })
end
result.gsub('[0]', '[{{ $index }}]').gsub('_0_', '_{{ $index }}_')
end
end
result.gsub('[0]', '[{{ $index }}]').gsub('_0_', '_{{ $index }}_')
end
end

View File

@@ -1,3 +1,9 @@
class Api::Admin::EnterpriseRelationshipPermissionSerializer < ActiveModel::Serializer
attributes :id, :name
# frozen_string_literal: true
module Api
module Admin
class EnterpriseRelationshipPermissionSerializer < ActiveModel::Serializer
attributes :id, :name
end
end
end

View File

@@ -1,13 +1,19 @@
class Api::Admin::EnterpriseRelationshipSerializer < ActiveModel::Serializer
attributes :id, :parent_id, :parent_name, :child_id, :child_name
# frozen_string_literal: true
has_many :permissions
module Api
module Admin
class EnterpriseRelationshipSerializer < ActiveModel::Serializer
attributes :id, :parent_id, :parent_name, :child_id, :child_name
def parent_name
object.parent.name
end
has_many :permissions
def child_name
object.child.name
def parent_name
object.parent.name
end
def child_name
object.child.name
end
end
end
end

View File

@@ -1,11 +1,17 @@
class Api::Admin::EnterpriseRoleSerializer < ActiveModel::Serializer
attributes :id, :user_id, :enterprise_id, :user_email, :enterprise_name
# frozen_string_literal: true
def user_email
object.user.email
end
module Api
module Admin
class EnterpriseRoleSerializer < ActiveModel::Serializer
attributes :id, :user_id, :enterprise_id, :user_email, :enterprise_name
def enterprise_name
object.enterprise.name
def user_email
object.user.email
end
def enterprise_name
object.enterprise.name
end
end
end
end

View File

@@ -1,82 +1,92 @@
class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer
attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category, :permalink,
:payment_method_ids, :shipping_method_ids, :producer_profile_only, :long_description,
:preferred_shopfront_message, :preferred_shopfront_closed_message,
:preferred_shopfront_taxon_order, :preferred_shopfront_order_cycle_order,
:preferred_product_selection_from_inventory_only,
:preferred_show_customer_names_to_suppliers, :owner, :contact, :users, :tag_groups,
:default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes,
:logo, :promo_image, :terms_and_conditions,
:terms_and_conditions_file_name, :terms_and_conditions_updated_at
# frozen_string_literal: true
has_one :owner, serializer: Api::Admin::UserSerializer
has_many :users, serializer: Api::Admin::UserSerializer
has_one :address, serializer: Api::AddressSerializer
module Api
module Admin
class EnterpriseSerializer < ActiveModel::Serializer
attributes :name, :id, :is_primary_producer, :is_distributor, :sells, :category, :permalink,
:payment_method_ids, :shipping_method_ids, :producer_profile_only,
:long_description, :preferred_product_selection_from_inventory_only,
:preferred_shopfront_message, :preferred_shopfront_closed_message,
:preferred_shopfront_taxon_order, :preferred_shopfront_order_cycle_order,
:preferred_show_customer_names_to_suppliers, :owner, :contact, :users, :tag_groups,
:default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes,
:logo, :promo_image, :terms_and_conditions,
:terms_and_conditions_file_name, :terms_and_conditions_updated_at
def logo
attachment_urls(object.logo, [:thumb, :small, :medium])
end
has_one :owner, serializer: Api::Admin::UserSerializer
has_many :users, serializer: Api::Admin::UserSerializer
has_one :address, serializer: Api::AddressSerializer
def promo_image
attachment_urls(object.promo_image, [:thumb, :medium, :large])
end
def terms_and_conditions
return unless object.terms_and_conditions.file?
object.terms_and_conditions.url
end
def terms_and_conditions_updated_at
object.terms_and_conditions_updated_at&.to_s
end
def tag_groups
object.tag_rules.prioritised.reject(&:is_default).each_with_object([]) do |tag_rule, tag_groups|
tag_group = find_match(tag_groups, tag_rule.preferred_customer_tags.
split(",").
map{ |t| { text: t } })
if tag_group[:rules].blank?
tag_groups << tag_group
tag_group[:position] = tag_groups.count
def logo
attachment_urls(object.logo, [:thumb, :small, :medium])
end
tag_group[:rules] << Api::Admin::TagRuleSerializer.new(tag_rule).serializable_hash
end
end
def default_tag_group
default_rules = object.tag_rules.select(&:is_default)
serialized_rules =
ActiveModel::ArraySerializer.new(default_rules,
each_serializer: Api::Admin::TagRuleSerializer)
{ tags: [], rules: serialized_rules }
end
def promo_image
attachment_urls(object.promo_image, [:thumb, :medium, :large])
end
def find_match(tag_groups, tags)
tag_groups.each do |tag_group|
return tag_group if tag_group[:tags].length == tags.length &&
(tag_group[:tags] & tags) == tag_group[:tags]
end
{ tags: tags, rules: [] }
end
def terms_and_conditions
return unless object.terms_and_conditions.file?
private
object.terms_and_conditions.url
end
# Returns a hash of URLs for specified versions of an attachment.
#
# Example:
#
# attachment_urls(object.logo, [:thumb, :small, :medium])
# # {
# # thumb: LOGO_THUMB_URL,
# # small: LOGO_SMALL_URL,
# # medium: LOGO_MEDIUM_URL
# # }
def attachment_urls(attachment, versions)
return unless attachment.file?
def terms_and_conditions_updated_at
object.terms_and_conditions_updated_at&.to_s
end
versions.each_with_object({}) do |version, urls|
urls[version] = attachment.url(version)
def tag_groups
prioritized_tag_rules.each_with_object([]) do |tag_rule, tag_groups|
tag_group = find_match(tag_groups, tag_rule.preferred_customer_tags.
split(",").
map{ |t| { text: t } })
if tag_group[:rules].blank?
tag_groups << tag_group
tag_group[:position] = tag_groups.count
end
tag_group[:rules] << Api::Admin::TagRuleSerializer.new(tag_rule).serializable_hash
end
end
def default_tag_group
default_rules = object.tag_rules.select(&:is_default)
serialized_rules =
ActiveModel::ArraySerializer.new(default_rules,
each_serializer: Api::Admin::TagRuleSerializer)
{ tags: [], rules: serialized_rules }
end
def find_match(tag_groups, tags)
tag_groups.each do |tag_group|
return tag_group if tag_group[:tags].length == tags.length &&
(tag_group[:tags] & tags) == tag_group[:tags]
end
{ tags: tags, rules: [] }
end
private
def prioritized_tag_rules
object.tag_rules.prioritised.reject(&:is_default)
end
# Returns a hash of URLs for specified versions of an attachment.
#
# Example:
#
# attachment_urls(object.logo, [:thumb, :small, :medium])
# # {
# # thumb: LOGO_THUMB_URL,
# # small: LOGO_SMALL_URL,
# # medium: LOGO_MEDIUM_URL
# # }
def attachment_urls(attachment, versions)
return unless attachment.file?
versions.each_with_object({}) do |version, urls|
urls[version] = attachment.url(version)
end
end
end
end
end

View File

@@ -1,48 +1,54 @@
class Api::Admin::ExchangeSerializer < ActiveModel::Serializer
attributes :id, :sender_id, :receiver_id, :incoming, :variants,
:receival_instructions, :pickup_time, :pickup_instructions,
:tags, :tag_list
# frozen_string_literal: true
has_many :enterprise_fees, serializer: Api::Admin::BasicEnterpriseFeeSerializer
module Api
module Admin
class ExchangeSerializer < ActiveModel::Serializer
attributes :id, :sender_id, :receiver_id, :incoming, :variants,
:receival_instructions, :pickup_time, :pickup_instructions,
:tags, :tag_list
def variants
variants = object.incoming? ? visible_incoming_variants : visible_outgoing_variants
Hash[object.variants.merge(variants).map { |v| [v.id, true] }]
end
has_many :enterprise_fees, serializer: Api::Admin::BasicEnterpriseFeeSerializer
private
def variants
variants = object.incoming? ? visible_incoming_variants : visible_outgoing_variants
Hash[object.variants.merge(variants).map { |v| [v.id, true] }]
end
def visible_incoming_variants
if object.order_cycle.prefers_product_selection_from_coordinator_inventory_only?
permitted_incoming_variants.visible_for(object.order_cycle.coordinator)
else
permitted_incoming_variants
private
def visible_incoming_variants
if object.order_cycle.prefers_product_selection_from_coordinator_inventory_only?
permitted_incoming_variants.visible_for(object.order_cycle.coordinator)
else
permitted_incoming_variants
end
end
def visible_outgoing_variants
if object.receiver.prefers_product_selection_from_inventory_only?
permitted_outgoing_variants.visible_for(object.receiver)
else
permitted_outgoing_variants.not_hidden_for(object.receiver)
end
end
def permitted_incoming_variants
OpenFoodNetwork::OrderCyclePermissions.new(options[:current_user], object.order_cycle).
visible_variants_for_incoming_exchanges_from(object.sender)
end
def permitted_outgoing_variants
OpenFoodNetwork::OrderCyclePermissions.new(options[:current_user], object.order_cycle)
.visible_variants_for_outgoing_exchanges_to(object.receiver)
end
def tag_list
object.tag_list.join(",")
end
def tags
object.tag_list.map{ |t| { text: t } }
end
end
end
def visible_outgoing_variants
if object.receiver.prefers_product_selection_from_inventory_only?
permitted_outgoing_variants.visible_for(object.receiver)
else
permitted_outgoing_variants.not_hidden_for(object.receiver)
end
end
def permitted_incoming_variants
OpenFoodNetwork::OrderCyclePermissions.new(options[:current_user], object.order_cycle).
visible_variants_for_incoming_exchanges_from(object.sender)
end
def permitted_outgoing_variants
OpenFoodNetwork::OrderCyclePermissions.new(options[:current_user], object.order_cycle)
.visible_variants_for_outgoing_exchanges_to(object.receiver)
end
def tag_list
object.tag_list.join(",")
end
def tags
object.tag_list.map{ |t| { text: t } }
end
end

View File

@@ -1,46 +1,54 @@
# frozen_string_literal: true
require 'open_food_network/enterprise_issue_validator'
class Api::Admin::ForOrderCycle::EnterpriseSerializer < ActiveModel::Serializer
attributes :id, :name, :managed,
:issues_summary_supplier, :issues_summary_distributor,
:is_primary_producer, :is_distributor, :sells
module Api
module Admin
module ForOrderCycle
class EnterpriseSerializer < ActiveModel::Serializer
attributes :id, :name, :managed,
:issues_summary_supplier, :issues_summary_distributor,
:is_primary_producer, :is_distributor, :sells
def issues_summary_supplier
issues =
OpenFoodNetwork::EnterpriseIssueValidator.
new(object).
issues_summary(confirmation_only: true)
def issues_summary_supplier
issues =
OpenFoodNetwork::EnterpriseIssueValidator.
new(object).
issues_summary(confirmation_only: true)
if issues.nil? && products.empty?
issues = "no products in inventory"
if issues.nil? && products.empty?
issues = "no products in inventory"
end
issues
end
def issues_summary_distributor
OpenFoodNetwork::EnterpriseIssueValidator.new(object).issues_summary
end
def managed
Enterprise.managed_by(options[:spree_current_user]).include? object
end
private
def products_scope
products_relation = object.supplied_products
if order_cycle.prefers_product_selection_from_coordinator_inventory_only?
products_relation = products_relation.
visible_for(order_cycle.coordinator)
end
products_relation
end
def products
@products ||= products_scope
end
def order_cycle
options[:order_cycle]
end
end
end
issues
end
def issues_summary_distributor
OpenFoodNetwork::EnterpriseIssueValidator.new(object).issues_summary
end
def managed
Enterprise.managed_by(options[:spree_current_user]).include? object
end
private
def products_scope
products_relation = object.supplied_products
if order_cycle.prefers_product_selection_from_coordinator_inventory_only?
products_relation = products_relation.
visible_for(order_cycle.coordinator)
end
products_relation
end
def products
@products ||= products_scope
end
def order_cycle
options[:order_cycle]
end
end

View File

@@ -1,31 +1,39 @@
class Api::Admin::ForOrderCycle::SuppliedProductSerializer < ActiveModel::Serializer
attributes :name, :supplier_name, :image_url, :master_id, :variants
# frozen_string_literal: true
def supplier_name
object.supplier.andand.name
end
module Api
module Admin
module ForOrderCycle
class SuppliedProductSerializer < ActiveModel::Serializer
attributes :name, :supplier_name, :image_url, :master_id, :variants
def image_url
object.images.present? ? object.images.first.attachment.url(:mini) : nil
end
def supplier_name
object.supplier.andand.name
end
def master_id
object.master.id
end
def image_url
object.images.present? ? object.images.first.attachment.url(:mini) : nil
end
def variants
variants = if order_cycle.present? &&
order_cycle.prefers_product_selection_from_coordinator_inventory_only?
object.variants.visible_for(order_cycle.coordinator)
else
object.variants
end
variants.map { |variant| { id: variant.id, label: variant.full_name } }
end
def master_id
object.master.id
end
private
def variants
variants = if order_cycle.present? &&
order_cycle.prefers_product_selection_from_coordinator_inventory_only?
object.variants.visible_for(order_cycle.coordinator)
else
object.variants
end
variants.map { |variant| { id: variant.id, label: variant.full_name } }
end
def order_cycle
options[:order_cycle]
private
def order_cycle
options[:order_cycle]
end
end
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
class IdEmailSerializer < ActiveModel::Serializer

View File

@@ -1,3 +1,9 @@
class Api::Admin::IdNameSerializer < ActiveModel::Serializer
attributes :id, :name
# frozen_string_literal: true
module Api
module Admin
class IdNameSerializer < ActiveModel::Serializer
attributes :id, :name
end
end
end

View File

@@ -1,3 +1,9 @@
class Api::Admin::IdSerializer < ActiveModel::Serializer
attributes :id
# frozen_string_literal: true
module Api
module Admin
class IdSerializer < ActiveModel::Serializer
attributes :id
end
end
end

View File

@@ -1,24 +1,30 @@
# frozen_string_literal: true
require 'open_food_network/enterprise_issue_validator'
class Api::Admin::IndexEnterpriseSerializer < ActiveModel::Serializer
attributes :name, :id, :permalink, :is_primary_producer, :sells, :producer_profile_only, :owned,
:edit_path, :issues, :warnings
module Api
module Admin
class IndexEnterpriseSerializer < ActiveModel::Serializer
attributes :name, :id, :permalink, :is_primary_producer, :sells,
:producer_profile_only, :owned, :edit_path, :issues, :warnings
def owned
return true if options[:spree_current_user].admin?
def owned
return true if options[:spree_current_user].admin?
object.owner == options[:spree_current_user]
end
object.owner == options[:spree_current_user]
end
def edit_path
edit_admin_enterprise_path(object)
end
def edit_path
edit_admin_enterprise_path(object)
end
def issues
OpenFoodNetwork::EnterpriseIssueValidator.new(object).issues
end
def issues
OpenFoodNetwork::EnterpriseIssueValidator.new(object).issues
end
def warnings
OpenFoodNetwork::EnterpriseIssueValidator.new(object).warnings
def warnings
OpenFoodNetwork::EnterpriseIssueValidator.new(object).warnings
end
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'open_food_network/order_cycle_permissions'
module Api

View File

@@ -1,3 +1,9 @@
class Api::Admin::InventoryItemSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :variant_id, :visible
# frozen_string_literal: true
module Api
module Admin
class InventoryItemSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :variant_id, :visible
end
end
end

View File

@@ -1,29 +1,35 @@
class Api::Admin::LineItemSerializer < ActiveModel::Serializer
attributes :id, :quantity, :max_quantity, :price, :supplier, :final_weight_volume,
:units_product, :units_variant
# frozen_string_literal: true
has_one :order, serializer: Api::Admin::IdSerializer
module Api
module Admin
class LineItemSerializer < ActiveModel::Serializer
attributes :id, :quantity, :max_quantity, :price, :supplier, :final_weight_volume,
:units_product, :units_variant
def supplier
{ id: object.product.supplier_id }
end
has_one :order, serializer: Api::Admin::IdSerializer
def units_product
Api::Admin::UnitsProductSerializer.new(object.product).serializable_hash
end
def supplier
{ id: object.product.supplier_id }
end
def units_variant
Api::Admin::UnitsVariantSerializer.new(object.variant).serializable_hash
end
def units_product
Api::Admin::UnitsProductSerializer.new(object.product).serializable_hash
end
def final_weight_volume
object.final_weight_volume.to_f
end
def units_variant
Api::Admin::UnitsVariantSerializer.new(object.variant).serializable_hash
end
def max_quantity
return object.quantity unless object.max_quantity.present? &&
object.max_quantity > object.quantity
def final_weight_volume
object.final_weight_volume.to_f
end
object.max_quantity
def max_quantity
return object.quantity unless object.max_quantity.present? &&
object.max_quantity > object.quantity
object.max_quantity
end
end
end
end

View File

@@ -1,88 +1,95 @@
# frozen_string_literal: true
require 'open_food_network/order_cycle_permissions'
class Api::Admin::OrderCycleSerializer < ActiveModel::Serializer
attributes :id, :name, :orders_open_at, :orders_close_at, :coordinator_id, :exchanges,
:editable_variants_for_incoming_exchanges, :editable_variants_for_outgoing_exchanges,
:visible_variants_for_outgoing_exchanges,
:viewing_as_coordinator, :schedule_ids, :subscriptions_count
module Api
module Admin
class OrderCycleSerializer < ActiveModel::Serializer
attributes :id, :name, :orders_open_at, :orders_close_at, :coordinator_id, :exchanges,
:editable_variants_for_incoming_exchanges,
:editable_variants_for_outgoing_exchanges,
:visible_variants_for_outgoing_exchanges,
:viewing_as_coordinator, :schedule_ids, :subscriptions_count
has_many :coordinator_fees, serializer: Api::IdSerializer
has_many :coordinator_fees, serializer: Api::IdSerializer
def orders_open_at
object.orders_open_at.to_s
end
def orders_open_at
object.orders_open_at.to_s
end
def orders_close_at
object.orders_close_at.to_s
end
def orders_close_at
object.orders_close_at.to_s
end
def viewing_as_coordinator
Enterprise.managed_by(options[:current_user]).include? object.coordinator
end
def viewing_as_coordinator
Enterprise.managed_by(options[:current_user]).include? object.coordinator
end
def subscriptions_count
ProxyOrder.not_canceled.where(order_cycle_id: object.id).count
end
def subscriptions_count
ProxyOrder.not_canceled.where(order_cycle_id: object.id).count
end
def exchanges
scoped_exchanges = permissions.visible_exchanges.by_enterprise_name
def exchanges
scoped_exchanges = permissions.visible_exchanges.by_enterprise_name
ActiveModel::ArraySerializer.
new(scoped_exchanges, each_serializer: Api::Admin::ExchangeSerializer,
current_user: options[:current_user])
end
ActiveModel::ArraySerializer.
new(scoped_exchanges, each_serializer: Api::Admin::ExchangeSerializer,
current_user: options[:current_user])
end
def editable_variants_for_incoming_exchanges
variant_ids_by_supplier_id(permissions.all_incoming_editable_variants.all)
end
def editable_variants_for_incoming_exchanges
variant_ids_by_supplier_id(permissions.all_incoming_editable_variants.all)
end
def editable_variants_for_outgoing_exchanges
# For each enterprise that the current user is able to see in this order cycle,
# work out which variants should be editable within incoming exchanges from that enterprise
editable = {}
visible_enterprises.each do |enterprise|
variants = permissions.editable_variants_for_outgoing_exchanges_to(enterprise).pluck(:id)
editable[enterprise.id] = variants if variants.any?
end
editable
end
def editable_variants_for_outgoing_exchanges
# For each enterprise that the current user is able to see in this order cycle,
# work out which variants should be editable within incoming exchanges from that enterprise
editable = {}
visible_enterprises.each do |enterprise|
variants = permissions.editable_variants_for_outgoing_exchanges_to(enterprise).pluck(:id)
editable[enterprise.id] = variants if variants.any?
end
editable
end
def visible_variants_for_outgoing_exchanges
# For each enterprise that the current user is able to see in this order cycle,
# work out which variants should be visible within outgoing exchanges from that enterprise
visible = {}
visible_enterprises.each do |enterprise|
# This is hopefully a temporary measure, pending the arrival of multiple named inventories
# for shops. We need this here to allow hubs to restrict visible variants to only those in
# their inventory if they so choose
variants = if enterprise.prefers_product_selection_from_inventory_only?
permissions.
visible_variants_for_outgoing_exchanges_to(enterprise).
visible_for(enterprise)
else
permissions.
visible_variants_for_outgoing_exchanges_to(enterprise).
not_hidden_for(enterprise)
end.pluck(:id)
visible[enterprise.id] = variants if variants.any?
end
visible
end
def visible_variants_for_outgoing_exchanges
# For each enterprise that the current user is able to see in this order cycle,
# work out which variants should be visible within outgoing exchanges from that enterprise
visible = {}
visible_enterprises.each do |enterprise|
# This is hopefully a temporary measure, pending the arrival of multiple named inventories
# for shops. We need this here to allow hubs to restrict visible variants to only those in
# their inventory if they so choose
variants = if enterprise.prefers_product_selection_from_inventory_only?
permissions.
visible_variants_for_outgoing_exchanges_to(enterprise).
visible_for(enterprise)
else
permissions.
visible_variants_for_outgoing_exchanges_to(enterprise).
not_hidden_for(enterprise)
end.pluck(:id)
visible[enterprise.id] = variants if variants.any?
end
visible
end
private
private
def permissions
@permissions ||= OpenFoodNetwork::OrderCyclePermissions.new(options[:current_user], object)
end
def permissions
@permissions ||= OpenFoodNetwork::OrderCyclePermissions.new(options[:current_user], object)
end
def visible_enterprises
@visible_enterprises ||= permissions.visible_enterprises
end
def visible_enterprises
@visible_enterprises ||= permissions.visible_enterprises
end
def variant_ids_by_supplier_id(variants)
grouped_by_supplier = variants.group_by(&:supplier_id)
grouped_by_supplier.each do |supplier_id, grouped_variants|
grouped_by_supplier[supplier_id] = grouped_variants.map(&:id)
def variant_ids_by_supplier_id(variants)
grouped_by_supplier = variants.group_by(&:supplier_id)
grouped_by_supplier.each do |supplier_id, grouped_variants|
grouped_by_supplier[supplier_id] = grouped_variants.map(&:id)
end
end
end
end
end

View File

@@ -1,71 +1,77 @@
class Api::Admin::OrderSerializer < ActiveModel::Serializer
attributes :id, :number, :user_id, :full_name, :email, :phone, :completed_at, :display_total,
:edit_path, :state, :payment_state, :shipment_state,
:payments_path, :ready_to_ship, :ready_to_capture, :created_at,
:distributor_name, :special_instructions,
:item_total, :adjustment_total, :payment_total, :total, :display_outstanding_balance
# frozen_string_literal: true
has_one :distributor, serializer: Api::Admin::IdSerializer
has_one :order_cycle, serializer: Api::Admin::IdSerializer
module Api
module Admin
class OrderSerializer < ActiveModel::Serializer
attributes :id, :number, :user_id, :full_name, :email, :phone, :completed_at, :display_total,
:edit_path, :state, :payment_state, :shipment_state,
:payments_path, :ready_to_ship, :ready_to_capture, :created_at,
:distributor_name, :special_instructions, :display_outstanding_balance,
:item_total, :adjustment_total, :payment_total, :total
def full_name
object.billing_address.nil? ? "" : ( object.billing_address.full_name || "" )
end
has_one :distributor, serializer: Api::Admin::IdSerializer
has_one :order_cycle, serializer: Api::Admin::IdSerializer
def distributor_name
object.distributor.andand.name
end
def full_name
object.billing_address.nil? ? "" : ( object.billing_address.full_name || "" )
end
def display_outstanding_balance
return "" if object.outstanding_balance.zero?
def distributor_name
object.distributor.andand.name
end
object.display_outstanding_balance.to_s
end
def display_outstanding_balance
return "" if object.outstanding_balance.zero?
def edit_path
return '' unless object.id
object.display_outstanding_balance.to_s
end
spree_routes_helper.edit_admin_order_path(object)
end
def edit_path
return '' unless object.id
def payments_path
return '' unless object.payment_state
spree_routes_helper.edit_admin_order_path(object)
end
spree_routes_helper.admin_order_payments_path(object)
end
def payments_path
return '' unless object.payment_state
def ready_to_capture
pending_payment = object.pending_payments.first
object.payment_required? && pending_payment
end
spree_routes_helper.admin_order_payments_path(object)
end
def ready_to_ship
object.ready_to_ship?
end
def ready_to_capture
pending_payment = object.pending_payments.first
object.payment_required? && pending_payment
end
def display_total
object.display_total.to_html
end
def ready_to_ship
object.ready_to_ship?
end
def email
object.email || ""
end
def display_total
object.display_total.to_html
end
def phone
object.billing_address.nil? ? "a" : ( object.billing_address.phone || "" )
end
def email
object.email || ""
end
def created_at
object.created_at.blank? ? "" : I18n.l(object.created_at, format: '%B %d, %Y')
end
def phone
object.billing_address.nil? ? "a" : ( object.billing_address.phone || "" )
end
def completed_at
object.completed_at.blank? ? "" : I18n.l(object.completed_at, format: '%B %d, %Y')
end
def created_at
object.created_at.blank? ? "" : I18n.l(object.created_at, format: '%B %d, %Y')
end
private
def completed_at
object.completed_at.blank? ? "" : I18n.l(object.completed_at, format: '%B %d, %Y')
end
def spree_routes_helper
Spree::Core::Engine.routes.url_helpers
private
def spree_routes_helper
Spree::Core::Engine.routes.url_helpers
end
end
end
end

View File

@@ -1,13 +1,19 @@
module Api::Admin::PaymentMethod
class BaseSerializer < ActiveModel::Serializer
attributes :id, :name, :type, :tag_list, :tags
# frozen_string_literal: true
def tag_list
object.tag_list.join(",")
end
module Api
module Admin
module PaymentMethod
class BaseSerializer < ActiveModel::Serializer
attributes :id, :name, :type, :tag_list, :tags
def tags
object.tag_list.map{ |t| { text: t } }
def tag_list
object.tag_list.join(",")
end
def tags
object.tag_list.map{ |t| { text: t } }
end
end
end
end
end

View File

@@ -1,5 +1,11 @@
module Api::Admin::PaymentMethod
class StripeSerializer < BaseSerializer
attributes :preferred_enterprise_id
# frozen_string_literal: true
module Api
module Admin
module PaymentMethod
class StripeSerializer < BaseSerializer
attributes :preferred_enterprise_id
end
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
class PaymentMethodSerializer < ActiveModel::Serializer

View File

@@ -1,44 +1,50 @@
class Api::Admin::ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name,
:inherits_properties, :on_hand, :price, :available_on, :permalink_live,
:tax_category_id, :import_date, :image_url, :thumb_url
# frozen_string_literal: true
has_one :supplier, key: :producer_id, embed: :id
has_one :primary_taxon, key: :category_id, embed: :id
has_many :variants, key: :variants, serializer: Api::Admin::VariantSerializer
has_one :master, serializer: Api::Admin::VariantSerializer
module Api
module Admin
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :sku, :variant_unit, :variant_unit_scale, :variant_unit_name,
:inherits_properties, :on_hand, :price, :available_on, :permalink_live,
:tax_category_id, :import_date, :image_url, :thumb_url
def image_url
if object.images.present?
object.images.first.attachment.url(:product)
else
"/noimage/product.png"
has_one :supplier, key: :producer_id, embed: :id
has_one :primary_taxon, key: :category_id, embed: :id
has_many :variants, key: :variants, serializer: Api::Admin::VariantSerializer
has_one :master, serializer: Api::Admin::VariantSerializer
def image_url
if object.images.present?
object.images.first.attachment.url(:product)
else
"/noimage/product.png"
end
end
def thumb_url
if object.images.present?
object.images.first.attachment.url(:mini)
else
"/noimage/mini.png"
end
end
def on_hand
return 0 if object.on_hand.nil?
object.on_hand
end
def price
object.price.nil? ? '0.0' : object.price
end
def available_on
object.available_on.blank? ? "" : object.available_on.strftime("%F %T")
end
def permalink_live
object.permalink
end
end
end
def thumb_url
if object.images.present?
object.images.first.attachment.url(:mini)
else
"/noimage/mini.png"
end
end
def on_hand
return 0 if object.on_hand.nil?
object.on_hand
end
def price
object.price.nil? ? '0.0' : object.price
end
def available_on
object.available_on.blank? ? "" : object.available_on.strftime("%F %T")
end
def permalink_live
object.permalink
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
class ProxyOrderSerializer < ActiveModel::Serializer

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
class ScheduleSerializer < ActiveModel::Serializer

View File

@@ -1,11 +1,17 @@
class Api::Admin::ShippingMethodSerializer < ActiveModel::Serializer
attributes :id, :name, :tag_list, :tags
# frozen_string_literal: true
def tag_list
object.tag_list.join(",")
end
module Api
module Admin
class ShippingMethodSerializer < ActiveModel::Serializer
attributes :id, :name, :tag_list, :tags
def tags
object.tag_list.map{ |t| { text: t } }
def tag_list
object.tag_list.join(",")
end
def tags
object.tag_list.map{ |t| { text: t } }
end
end
end
end

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
# Used by admin subscription form

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
class SubscriptionLineItemSerializer < ActiveModel::Serializer

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
module Api
module Admin
class SubscriptionSerializer < ActiveModel::Serializer

View File

@@ -1,51 +1,63 @@
class Api::Admin::TagRuleSerializer < ActiveModel::Serializer
delegate :serializable_hash, to: :rule_specific_serializer
# frozen_string_literal: true
def rule_specific_serializer
"Api::Admin::#{object.class}Serializer".constantize.new(object)
end
end
module Api
module Admin
class TagRuleSerializer < ActiveModel::Serializer
delegate :serializable_hash, to: :rule_specific_serializer
module Api::Admin::TagRule
class BaseSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :type, :is_default, :preferred_customer_tags
end
class DiscountOrderSerializer < BaseSerializer
has_one :calculator, serializer: Api::Admin::Calculator::FlatPercentItemTotalSerializer
end
class FilterShippingMethodsSerializer < BaseSerializer
attributes :preferred_matched_shipping_methods_visibility, :preferred_shipping_method_tags,
:shipping_method_tags
def shipping_method_tags
object.preferred_shipping_method_tags.split(",")
end
end
class FilterPaymentMethodsSerializer < BaseSerializer
attributes :preferred_matched_payment_methods_visibility, :preferred_payment_method_tags,
:payment_method_tags
def payment_method_tags
object.preferred_payment_method_tags.split(",")
end
end
class FilterProductsSerializer < BaseSerializer
attributes :preferred_matched_variants_visibility, :preferred_variant_tags, :variant_tags
def variant_tags
object.preferred_variant_tags.split(",")
end
end
class FilterOrderCyclesSerializer < BaseSerializer
attributes :preferred_matched_order_cycles_visibility, :preferred_exchange_tags, :exchange_tags
def exchange_tags
object.preferred_exchange_tags.split(",")
def rule_specific_serializer
"Api::Admin::#{object.class}Serializer".constantize.new(object)
end
end
end
end
module Api
module Admin
module TagRule
class BaseSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :type, :is_default, :preferred_customer_tags
end
class DiscountOrderSerializer < BaseSerializer
has_one :calculator, serializer: Api::Admin::Calculator::FlatPercentItemTotalSerializer
end
class FilterShippingMethodsSerializer < BaseSerializer
attributes :preferred_matched_shipping_methods_visibility, :preferred_shipping_method_tags,
:shipping_method_tags
def shipping_method_tags
object.preferred_shipping_method_tags.split(",")
end
end
class FilterPaymentMethodsSerializer < BaseSerializer
attributes :preferred_matched_payment_methods_visibility, :preferred_payment_method_tags,
:payment_method_tags
def payment_method_tags
object.preferred_payment_method_tags.split(",")
end
end
class FilterProductsSerializer < BaseSerializer
attributes :preferred_matched_variants_visibility, :preferred_variant_tags, :variant_tags
def variant_tags
object.preferred_variant_tags.split(",")
end
end
class FilterOrderCyclesSerializer < BaseSerializer
attributes :preferred_matched_order_cycles_visibility,
:preferred_exchange_tags,
:exchange_tags
def exchange_tags
object.preferred_exchange_tags.split(",")
end
end
end
end
end

View File

@@ -1,3 +1,9 @@
class Api::Admin::TaxCategorySerializer < ActiveModel::Serializer
attributes :id, :name
# frozen_string_literal: true
module Api
module Admin
class TaxCategorySerializer < ActiveModel::Serializer
attributes :id, :name
end
end
end

View File

@@ -1,3 +1,9 @@
class Api::Admin::TaxonSerializer < ActiveModel::Serializer
attributes :id, :name, :pretty_name
# frozen_string_literal: true
module Api
module Admin
class TaxonSerializer < ActiveModel::Serializer
attributes :id, :name, :pretty_name
end
end
end

View File

@@ -1,3 +1,9 @@
class Api::Admin::UnitsProductSerializer < ActiveModel::Serializer
attributes :id, :name, :group_buy_unit_size, :variant_unit
# frozen_string_literal: true
module Api
module Admin
class UnitsProductSerializer < ActiveModel::Serializer
attributes :id, :name, :group_buy_unit_size, :variant_unit
end
end
end

View File

@@ -1,8 +1,14 @@
class Api::Admin::UnitsVariantSerializer < ActiveModel::Serializer
attributes :id, :full_name, :unit_value
# frozen_string_literal: true
def full_name
full_name = object.full_name
object.product.name + (full_name.blank? ? "" : ": #{full_name}")
module Api
module Admin
class UnitsVariantSerializer < ActiveModel::Serializer
attributes :id, :full_name, :unit_value
def full_name
full_name = object.full_name
object.product.name + (full_name.blank? ? "" : ": #{full_name}")
end
end
end
end

View File

@@ -1,20 +1,26 @@
# frozen_string_literal: true
require 'open_food_network/address_finder'
class Api::Admin::UserSerializer < ActiveModel::Serializer
attributes :id, :email, :confirmed
module Api
module Admin
class UserSerializer < ActiveModel::Serializer
attributes :id, :email, :confirmed
has_one :ship_address, serializer: Api::AddressSerializer
has_one :bill_address, serializer: Api::AddressSerializer
has_one :ship_address, serializer: Api::AddressSerializer
has_one :bill_address, serializer: Api::AddressSerializer
def ship_address
OpenFoodNetwork::AddressFinder.new(object.email, object).ship_address
end
def ship_address
OpenFoodNetwork::AddressFinder.new(object.email, object).ship_address
end
def bill_address
OpenFoodNetwork::AddressFinder.new(object.email, object).bill_address
end
def bill_address
OpenFoodNetwork::AddressFinder.new(object.email, object).bill_address
end
def confirmed
object.confirmed?
def confirmed
object.confirmed?
end
end
end
end

View File

@@ -1,12 +1,18 @@
class Api::Admin::VariantOverrideSerializer < ActiveModel::Serializer
attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand, :default_stock,
:resettable, :tag_list, :tags, :import_date
# frozen_string_literal: true
def tag_list
object.tag_list.join(",")
end
module Api
module Admin
class VariantOverrideSerializer < ActiveModel::Serializer
attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand,
:default_stock, :resettable, :tag_list, :tags, :import_date
def tags
object.tag_list.map { |t| { text: t } }
def tag_list
object.tag_list.join(",")
end
def tags
object.tag_list.map { |t| { text: t } }
end
end
end
end

View File

@@ -1,53 +1,60 @@
class Api::Admin::VariantSerializer < ActiveModel::Serializer
attributes :id, :name, :producer_name, :image, :sku, :import_date,
:options_text, :unit_value, :unit_description, :unit_to_display,
:display_as, :display_name, :name_to_display,
:price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name
# frozen_string_literal: true
has_many :variant_overrides
module Api
module Admin
class VariantSerializer < ActiveModel::Serializer
attributes :id, :name, :producer_name, :image, :sku, :import_date,
:options_text, :unit_value, :unit_description, :unit_to_display,
:display_as, :display_name, :name_to_display,
:price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name
def name
if object.full_name.present?
"#{object.name} - #{object.full_name}"
else
object.name
has_many :variant_overrides
def name
if object.full_name.present?
"#{object.name} - #{object.full_name}"
else
object.name
end
end
def on_hand
return 0 if object.on_hand.nil?
object.on_hand
end
def price
# Decimals are passed to json as strings,
# we need to run parseFloat.toFixed(2) on the client.
object.price.nil? ? 0.to_f : object.price
end
def producer_name
object.product.supplier.name
end
def image
return if object.product.images.empty?
object.product.images.first.mini_url
end
def in_stock
object.in_stock?
end
def stock_location_id
return if object.stock_items.empty?
object.stock_items.first.stock_location.id
end
def stock_location_name
return if object.stock_items.empty?
object.stock_items.first.stock_location.name
end
end
end
def on_hand
return 0 if object.on_hand.nil?
object.on_hand
end
def price
# Decimals are passed to json as strings, we need to run parseFloat.toFixed(2) on the client.
object.price.nil? ? 0.to_f : object.price
end
def producer_name
object.product.supplier.name
end
def image
return if object.product.images.empty?
object.product.images.first.mini_url
end
def in_stock
object.in_stock?
end
def stock_location_id
return if object.stock_items.empty?
object.stock_items.first.stock_location.id
end
def stock_location_name
return if object.stock_items.empty?
object.stock_items.first.stock_location.name
end
end