Sha256: 581022feac1b509f866491455bef2d80bc2b141fcc04f4f0d5b878b23ba1813e

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Sequel::Plugins::LuxLocalize
  module ClassMethods
    # in any modal
    # localize :name
    # to make :name multilang
    # and to create :name_en, :name_de, ...
    def localize(name)
      unless db_schema[name]
        puts "Field [:#{name}] does not exist in #{self.to_s}".red
      end

      unless (db_schema[name][:db_type] rescue nil) == 'jsonb'
        puts "Field [:#{name}] in #{self.to_s} is not of type [jsonb]".red
      end

      class_eval %[
        def #{name}(locale=nil)
          field = self[:#{name}]
          field ||= {}
          locale ||= Locale.current
          return field[locale] if field[locale] && !field[locale].start_with?('?')
          val = field[Locale::DEFAULT].to_s
          Lux.dev? ? '?'+val : val
        end

        def #{name}=(value)
          self[:#{name}] ||= {} # if self[:#{name}].class.to_s != 'Hash'
          self[:#{name}][Locale.current] = value
        end
      ]

      for lang in Locale.all
        class_eval %[
          def #{name}_#{lang}
            #{name}('#{lang}')
          end

          def #{name}_#{lang}=(value)
            value = nil if value.start_with?('?')
            self[:#{name}] ||= {}
            self[:#{name}]['#{lang}'] = value
          end
        ]
      end
    end
  end

  module InstanceMethods

  end

  def DatasetMethods

  end
end

Sequel::Model.plugin :lux_localize

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.1.35 ./lib/plugins/db_helpers/localize_plugin.rb
lux-fw-0.1.17 ./lib/plugins/db_helpers/localize_plugin.rb