Sha256: 9902c88612978e03073c7651a63a6e2b8ef32a3df14bbe176ab848012c53f774

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Locomotive
  module Steam

    class APIContentEntryService < ContentEntryService

      include Locomotive::Steam::Services::Concerns::Decorator

      attr_accessor_initialize :content_type_repository, :repository, :locale, :request

      def create(type_slug, attributes, as_json = false)
        with_form(type_slug, attributes, as_json) do |_attributes|
          @content_type.entries.create(_attributes)
        end
      end

      def update(type_slug, id_or_slug, attributes, as_json = false)
        with_form(type_slug, attributes, as_json) do |_attributes|
          entry = @content_type.entries.by_id_or_slug(id_or_slug).first

          entry.update_attributes(_attributes)

          entry
        end
      end

      private

      def with_form(type_slug, attributes, as_json, &block)
        load_content_type(type_slug)

        ::Mongoid::Fields::I18n.with_locale(self.locale) do
          form  = Locomotive::API::Forms::ContentEntryForm.new(@content_type, attributes)

          entry = yield(form.serializable_hash)

          make_entity(entry, as_json)
        end
      end

      def make_entity(entry, as_json)
        entity = entry.to_steam(@content_type)
        decorated_entity = Locomotive::Steam::Decorators::I18nDecorator.new(entity, locale)
        _json_decorate(decorated_entity, as_json)
      end

      def load_content_type(slug)
        @content_type = site.content_types.where(slug: slug).first
      end

      def site
        request.env['locomotive.site']
      end

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms-3.2.0.rc2 lib/locomotive/steam/services/api_content_entry_service.rb
locomotivecms-3.2.0.rc1 lib/locomotive/steam/services/api_content_entry_service.rb