Sha256: 0031bcd91c9ee87b9ab2ec2b0cd5b4ef0dd10dbecd4e280d737060795427d5d0

Contents?: true

Size: 1.49 KB

Versions: 11

Compression:

Stored size: 1.49 KB

Contents

module Locomotive

  class TranslationService < Struct.new(:site, :account)

    def all(options = {})
      query       = site.translations.ordered
      keywords    = prepare_keywords_statement(options[:q])
      completion  = prepare_completion_statement(options[:filter_by])

      # filtering
      query = if completion then query.and(*keywords, *completion) else query.where(keywords) end

      # pagination
      query
        .page(options[:page] || 1)
        .per(options[:per_page] || Locomotive.config.ui[:per_page])
    end

    def update(translation, values)
      translation.tap do
        translation.values = values
        translation.updated_by = account if account
        translation.save
      end
    end

    def bulk_destroy(ids)
      site.translations.where(:id.in => ids).map do |translation|
        translation.destroy
        translation._id
      end
    end

    protected

    def prepare_keywords_statement(keywords)
      return {} if keywords.blank?

      regexp = /.*#{keywords.split.map { |k| Regexp.escape(k) }.join('.*')}.*/i

      [{ '$or' => [
        { key: regexp },
        *site.locales.map { |l| { "values.#{l}" => regexp  } }
      ] }]
    end

    def prepare_completion_statement(filter_by)
      case filter_by
      when 'done'       then [{ completion: site.locales.size }]
      when 'partially'  then [{ :completion.lt => site.locales.size }, { :completion.gt => 0 }]
      when 'none'       then [{ completion: 0 }]
      else {}
      end
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 app/services/locomotive/translation_service.rb
locomotivecms-4.2.0.alpha1 app/services/locomotive/translation_service.rb
locomotivecms-4.1.1 app/services/locomotive/translation_service.rb
locomotivecms-4.1.0 app/services/locomotive/translation_service.rb
locomotivecms-4.1.0.rc1 app/services/locomotive/translation_service.rb
locomotivecms-4.0.3 app/services/locomotive/translation_service.rb
locomotivecms-4.0.2 app/services/locomotive/translation_service.rb
locomotivecms-4.0.1 app/services/locomotive/translation_service.rb
locomotivecms-4.0.0 app/services/locomotive/translation_service.rb
locomotivecms-4.0.0.rc0 app/services/locomotive/translation_service.rb
locomotivecms-4.0.0.alpha3 app/services/locomotive/translation_service.rb