Sha256: b506505ea20070be85abd149b085488417449e2f004a80b752341f823f8d4343

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

module Locomotive
  module Steam
    module Middlewares

      class MissingTranslations

        def initialize(app, opts = {})
          @app = app
        end

        def call(env)
          find_and_persist_missing_translations(env['locomotive.site']) do
            @app.call(env)
          end
        end

        def find_and_persist_missing_translations(site)
          translations = []

          subscription = ActiveSupport::Notifications.subscribe('steam.missing_translation') do |name, start, finish, id, payload|
            translations << { key: payload[:input], values: { payload[:input] => false } }
          end

          yield.tap do
            ActiveSupport::Notifications.unsubscribe(subscription)
            persist_missing_translations(site, translations) unless translations.empty?
          end
        end

        def persist_missing_translations(site, translations)
          new_translations = []

          translations.each do |attributes|
            if existing = site.translations.where(key: attributes[:key]).first
              # don't need to call the DB if we already have the values in DB
              next if existing.values == attributes[:values]

              existing.update_attribute :values, existing.values.merge(attributes[:values])
            else
              new_translations << attributes
            end
          end

          site.translations.create(new_translations) unless new_translations.empty?
        end

      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.2.0.alpha1 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.1.1 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.1.0 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.1.0.rc1 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.0.3 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.0.2 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.0.1 lib/locomotive/steam/middlewares/missing_translations.rb
locomotivecms-4.0.0 lib/locomotive/steam/middlewares/missing_translations.rb