Sha256: b8090efd8d49a711348c74475d7d77f68d0ad88b68102cb91ed5079f91b84baf

Contents?: true

Size: 1.82 KB

Versions: 15

Compression:

Stored size: 1.82 KB

Contents

require 'sanitize'

module Locomotive
  module Steam

    class EntrySubmissionService

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

      attr_accessor_initialize :content_type_repository, :repository, :locale

      def submit(slug, attributes = {})
        type = get_type(slug)

        return nil if type.nil? || type.public_submission_enabled == false

        clean_attributes(attributes)

        build_entry(type, attributes) do |entry|
          if validate(entry)
            repository.create(entry)
          end
        end
      end

      def find(type_slug, slug)
        type = get_type(type_slug)

        return nil if type.nil?

        i18n_decorate { repository.with(type).by_slug(slug) }
      end

      def to_json(entry)
        return nil if entry.nil?

        entry.to_json
      end

      private

      def get_type(slug)
        return nil if slug.blank?

        content_type_repository.by_slug(slug)
      end

      def build_entry(type, attributes, &block)
        i18n_decorate { repository.with(type).build(attributes) }.tap do |entry|
          yield(entry)
        end
      end

      def validate(entry)
        # simple validations (existence of values) first
        entry.valid?

        # check if the entry has unique values for its
        # fields marked as unique
        content_type_repository.look_for_unique_fields(entry.content_type).each do |name, _|
          if repository.with(entry.content_type).exists?(name => entry.send(name))
            entry.errors.add(name, :unique)
          end
        end

        entry.errors.empty?
      end

      def clean_attributes(attributes)
        attributes.each do |key, value|
          next unless value.is_a?(String)
          attributes[key] = Sanitize.clean(value, Sanitize::Config::BASIC)
        end
      end

    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
locomotivecms_steam-1.1.2 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.1.1 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.1.0 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.1.0.rc3 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.1.0.rc2 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.1.0.rc1 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.1 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc4 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc3 lib/locomotive/steam/services/entry_submission_service.rb
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/services/entry_submission_service.rb