Sha256: 0d723a4bdb4296f9ed17ecce6a15a2dc09d7860af846661601072d3e7122aa3a

Contents?: true

Size: 1.38 KB

Versions: 51

Compression:

Stored size: 1.38 KB

Contents

class Marty::ConfigView < Marty::Grid
  has_marty_permissions create: :admin,
                        read: :admin,
                        update: :admin,
                        delete: :admin

  include Marty::Extras::Layout

  def configure(c)
    super

    c.title      = I18n.t('config', default: 'Config')
    c.model      = 'Marty::Config'
    c.attributes = [:key, :value, :description]
    c.editing    = :both
    c.store_config.merge!(sorters: [{ property: :key, direction: 'ASC' }])
  end

  def my_jsonb_getter
    lambda { |r| v = Marty::Config[r.key]; !v.nil? && v.to_json || '' }
  end

  def my_jsonb_pretty_getter
    lambda { |r|
      v = Marty::Config[r.key]
      !v.nil? && (JSON.pretty_generate(v) rescue v.to_json) || ''
    }
  end

  def my_jsonb_setter
    lambda { |r, v|
      return r.set_value(nil) if v.blank?

      decoded = ActiveSupport::JSON.decode(v) rescue nil
      r.set_value(decoded)
    }
  end

  def default_form_items
    [
      :key,
      jsonb_field(:value,
                  getter: my_jsonb_pretty_getter,
                  setter: my_jsonb_setter,
                 ),
      textarea_field(:description),
    ]
  end

  attribute :key do |c|
    c.flex = 1
  end

  column :value do |c|
    c.flex   = 3
    c.getter = my_jsonb_getter
    c.setter = my_jsonb_setter
  end

  attribute :description do |c|
    c.flex = 1
  end
end

ConfigView = Marty::ConfigView

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
marty-14.3.0 app/components/marty/config_view.rb
marty-14.0.0 app/components/marty/config_view.rb
marty-13.0.2 app/components/marty/config_view.rb
marty-11.0.0 app/components/marty/config_view.rb
marty-10.0.3 app/components/marty/config_view.rb
marty-10.0.2 app/components/marty/config_view.rb
marty-10.0.0 app/components/marty/config_view.rb
marty-9.5.1 app/components/marty/config_view.rb
marty-9.5.0 app/components/marty/config_view.rb
marty-9.3.3 app/components/marty/config_view.rb
marty-9.3.2 app/components/marty/config_view.rb
marty-9.3.0 app/components/marty/config_view.rb
marty-8.5.0 app/components/marty/config_view.rb
marty-8.4.1 app/components/marty/config_view.rb
marty-8.3.1 app/components/marty/config_view.rb
marty-8.2.0 app/components/marty/config_view.rb
marty-8.0.0 app/components/marty/config_view.rb
marty-6.1.0 app/components/marty/config_view.rb
marty-5.2.0 app/components/marty/config_view.rb
marty-5.1.4 app/components/marty/config_view.rb