Module: Marty::Extras::Layout

Constant Summary collapse

BOOL_MAP =

employ lots of hakery to implement NULLable boolean field in Netzke 8.x.

{
  nil   => '---',
  true  => 'True',
  false => 'False',
}
MAP_BOOL =
{
  '---'   => nil,
  ''      => nil,
  'True'  => true,
  'False' => false,
}

Instance Method Summary collapse

Instance Method Details

#bool_getter(name) ⇒ Object



138
139
140
# File 'app/components/marty/extras/layout.rb', line 138

def bool_getter(name)
  lambda { |r| BOOL_MAP[r.send(name)] }
end

#bool_setter(name) ⇒ Object



142
143
144
# File 'app/components/marty/extras/layout.rb', line 142

def bool_setter(name)
  lambda { |r, v| r.send("#{name}=", MAP_BOOL[v]) }
end

#dispfield(params = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'app/components/marty/extras/layout.rb', line 26

def dispfield(params = {})
  {
    attr_type: :displayfield,
    hide_label: !params[:field_label],
    read_only: true,
  }.merge(params)
end

#enum_array(c, klass) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/components/marty/extras/layout.rb', line 99

def enum_array(c, klass)
  editor_config = {
    multi_select: true,
    store:        klass.values.to_a.sort,
    type:         :string,
    xtype:        :combo,
  }
  c.merge!(
    type:          :string,
    column_config: { editor: editor_config },
    field_config:  editor_config,
  )
end

#enum_column(c, class_or_array, col = nil, allow_null = true) ⇒ Object

PG ENUM field handling



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/components/marty/extras/layout.rb', line 76

def enum_column(c, class_or_array, col = nil, allow_null = true)
  col ||= c.name.demodulize.tableize.singularize
  vals = class_or_array.is_a?(Array) ? class_or_array : class_or_array.values
  editor_config = {
    trigger_action: :all,
    xtype:          :combo,

    # hacky: extjs has issues with forceSelection true and clearing combos
    store:          vals + (allow_null ? ['---'] : []),

    # we can dynamically add enums (i.e. they're not in VALUES) --
    # turn off forced selection.
    # forceSelection: true,
  }
  c.merge!(
    column_config: { editor: editor_config },
    field_config:  editor_config,
    type:          :string,
    setter:        enum_setter(c.name),
    sorting_scope: get_sorter(col)
  )
end

#enum_setter(name) ⇒ Object



113
114
115
# File 'app/components/marty/extras/layout.rb', line 113

def enum_setter(name)
  lambda { |r, v| r.send("#{name}=", v.blank? || v == '---' ? nil : v) }
end

#fieldset(title, *args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'app/components/marty/extras/layout.rb', line 17

def fieldset(title, *args)
  params = args.pop
  params.merge(items: args,
               xtype: 'fieldset',
               defaults: { anchor: '100%' },
               title: title,
              )
end

#get_sorter(col) ⇒ Object



117
118
119
# File 'app/components/marty/extras/layout.rb', line 117

def get_sorter(col)
  lambda { |rel, dir| rel.order(Arel.sql("#{col}::text #{dir}")) }
end

#hbox(*args) ⇒ Object



3
4
5
6
7
8
# File 'app/components/marty/extras/layout.rb', line 3

def hbox(*args)
  params = args.pop
  params.merge(layout: { type: :hbox, align: :stretch },
               items: args,
              )
end

#hspacer(params = {}) ⇒ Object



38
39
40
# File 'app/components/marty/extras/layout.rb', line 38

def hspacer(params = {})
  hbox({ flex: 1, border: false }.merge(params))
end

#jsonb_field(name, options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/components/marty/extras/layout.rb', line 57

def jsonb_field(name, options = {})
  {
      name:        name,
      width:       '100%',
      height:      150,
      xtype:       :textareafield,
      auto_scroll: true,
      spellcheck:  false,
      allow_blank: false,
      field_style: {
        font_family: 'courier new',
        font_size:   '12px'
      },
  }.merge(options)
end

#nullable_bool_column(name) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/components/marty/extras/layout.rb', line 146

def nullable_bool_column(name)
  editor_config = {
    trigger_action: :all,
    xtype:          :combo,
    store:          ['True', 'False', '---'],
  }
  {
    column_config: { editor: editor_config },
    field_config:  editor_config,
    type:          :string,
    getter:        bool_getter(name),
    setter:        bool_setter(name),
  }
end

#range_column(c, name) ⇒ Object



203
204
205
206
207
208
# File 'app/components/marty/extras/layout.rb', line 203

def range_column(c, name)
  c.getter = range_getter(name)
  c.setter = range_setter(name)
  c.width  = 80
  c.align  = 'right'
end

#range_field(name) ⇒ Object



195
196
197
198
199
200
201
# File 'app/components/marty/extras/layout.rb', line 195

def range_field(name)
  {
    name:   name,
    getter: range_getter(name),
    setter: range_setter(name),
  }
end

#range_getter(name, json_field = nil) ⇒ Object

make sure to validate range vals on the model (e.g. see rule.rb)



164
165
166
167
168
169
170
# File 'app/components/marty/extras/layout.rb', line 164

def range_getter(name, json_field = nil)
  if json_field
    lambda { |r| Marty::Util.pg_range_to_human(r.send(json_field)[name]) }
  else
    lambda { |r| Marty::Util.pg_range_to_human(r.send(name)) }
  end
end

#range_setter(name, json_field = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/components/marty/extras/layout.rb', line 172

def range_setter(name, json_field = nil)
  if json_field
    lambda do |r, v|
      v = nil if v.blank?

      cookedv = v && v.present? && (Marty::Util.human_to_pg_range(v) rescue v)
      h = r.send(json_field)
      if cookedv
        r.send("#{json_field}=", h + { name => cookedv })
      else
        h.delete(name)
        r.send("#{json_field}=", h)
      end
    end
  else
    lambda do |r, v|
      next r.send("#{name}=", nil) if v.blank?

      r.send("#{name}=", v && (Marty::Util.human_to_pg_range(v) rescue v))
    end
  end
end

#textarea_field(name, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/marty/extras/layout.rb', line 42

def textarea_field(name, options = {})
  {
    name:        name,
    width:       '100%',
    height:      150,
    xtype:       :textareafield,
    auto_scroll: true,
    spellcheck:  false,
    field_style: {
      font_family: 'courier new',
      font_size:   '12px'
    },
  } + options
end

#tooltip(s1, s2) ⇒ Object



210
211
212
# File 'app/components/marty/extras/layout.rb', line 210

def tooltip s1, s2
  "<span data-qtip=\"#{s2}\">#{s1}</span>"
end

#vbox(*args) ⇒ Object



10
11
12
13
14
15
# File 'app/components/marty/extras/layout.rb', line 10

def vbox(*args)
  params = args.pop
  params.merge(layout: { type: :vbox, align: :stretch },
               items: args,
              )
end

#vspacer(params = {}) ⇒ Object



34
35
36
# File 'app/components/marty/extras/layout.rb', line 34

def vspacer(params = {})
  vbox({ flex: 1, border: false }.merge(params))
end