Sha256: c2a0815a83f75f969a61e5d52e95612d38b068288dd5b2aeb0204b7af0bfd475
Contents?: true
Size: 1.9 KB
Versions: 11
Compression:
Stored size: 1.9 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) useTempfiles(attributes) ::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 useTempfiles(attributes) # kind of marshal/unmarshal mechanism :-) attributes.each do |key, value| if value.is_a?(Hash) && value['tempfile'].present? && value['tempfile'].is_a?(String) && value['filename'].present? attributes[key]['tempfile'] = File.open(attributes[key]['tempfile']) end 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
11 entries across 11 versions & 1 rubygems