From 9c5a5d94921afc875700aefd747dafe80152b38f Mon Sep 17 00:00:00 2001 From: wvengen Date: Sat, 13 Oct 2018 15:18:55 +0200 Subject: [PATCH] API v1 config endpoint --- app/controllers/api/v1/configs_controller.rb | 7 ++++ app/serializers/config_serializer.rb | 39 ++++++++++++++++++++ config/routes.rb | 1 + lib/foodsoft_config.rb | 3 ++ 4 files changed, 50 insertions(+) create mode 100644 app/controllers/api/v1/configs_controller.rb create mode 100644 app/serializers/config_serializer.rb diff --git a/app/controllers/api/v1/configs_controller.rb b/app/controllers/api/v1/configs_controller.rb new file mode 100644 index 000000000..3ba0ed4b3 --- /dev/null +++ b/app/controllers/api/v1/configs_controller.rb @@ -0,0 +1,7 @@ +class Api::V1::ConfigsController < Api::V1::BaseController + + def show + render json: FoodsoftConfig, serializer: ConfigSerializer, root: 'config' + end + +end diff --git a/app/serializers/config_serializer.rb b/app/serializers/config_serializer.rb new file mode 100644 index 000000000..a196a372d --- /dev/null +++ b/app/serializers/config_serializer.rb @@ -0,0 +1,39 @@ +class ConfigSerializer < ActiveModel::Serializer + + # details + attributes :name, :homepage, :contact + + # settings + attributes :currency_unit, :currency_space, :default_locale, :price_markup, + :tolerance_is_costly, :use_apple_points, :use_tolerance + + # layout + attributes :page_footer_html, :webstats_tracking_code_html + + # help and version + attributes :applepear_url, :help_url, :foodsoft + + + def foodsoft + { + version: Foodsoft::VERSION, + revision: Foodsoft::REVISION, + url: object[:foodsoft_url] + } + end + + def page_footer_html + # also see footer layout + if FoodsoftConfig[:page_footer].present? + FoodsoftConfig[:page_footer] + elsif FoodsoftConfig[:homepage].present? + ActionController::Base.helpers.link_to(FoodsoftConfig[:name], FoodsoftConfig[:homepage]) + else + FoodsoftConfig[:name] + end + end + + def webstats_tracking_code_html + FoodsoftConfig[:webstats_tracking_code].presence + end +end diff --git a/config/routes.rb b/config/routes.rb index 0dc225965..0f9dd5f25 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -246,6 +246,7 @@ namespace :api do namespace :v1 do resource :user, only: [:show] + resource :config, only: [:show] end end diff --git a/lib/foodsoft_config.rb b/lib/foodsoft_config.rb index 688872ce3..c4d4a3eba 100644 --- a/lib/foodsoft_config.rb +++ b/lib/foodsoft_config.rb @@ -180,6 +180,9 @@ def to_hash Hash[keys.map {|k| [k, self[k]]} ] end + # for using active_model_serializer in the api/v1/configs controller + alias :read_attribute_for_serialization :[] + protected # @!attribute default_config