BigW Consortium Gitlab

settings.rb 914 Bytes
Newer Older
1 2
module API
  class Settings < Grape::API
3
    before { authenticated_as_admin! }
4 5 6

    helpers do
      def current_settings
7 8
        @current_setting ||=
          (ApplicationSetting.current || ApplicationSetting.create_from_defaults)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
      end
    end

    # Get current applicaiton settings
    #
    # Example Request:
    #   GET /application/settings
    get "application/settings" do
      present current_settings, with: Entities::ApplicationSetting
    end

    # Modify applicaiton settings
    #
    # Example Request:
    #   PUT /application/settings
    put "application/settings" do
      attributes = current_settings.attributes.keys - ["id"]
      attrs = attributes_for_keys(attributes)

      if current_settings.update_attributes(attrs)
        present current_settings, with: Entities::ApplicationSetting
      else
        render_validation_error!(current_settings)
      end
    end
  end
end