Class: Ezframe::DataEditor

Inherits:
PageBase show all
Defined in:
lib/ezframe/editor.rb

Instance Attribute Summary

Attributes inherited from PageBase

#auth, #request

Instance Method Summary collapse

Methods inherited from PageBase

#login?, #make_base_url, #parse_json_body, #set_request, #show_base_template, #user, #warden

Constructor Details

#initialize(request = nil, model = nil) ⇒ DataEditor

Returns a new instance of DataEditor.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ezframe/editor.rb', line 6

def initialize(request=nil, model=nil)
  super(request, model)
  decide_target
  if model
    @column_set = @model.column_sets[@target]
    unless @column_set
      raise "[ERROR] no such column set: #{@target}"
    end
    @dataset = @column_set.dataset
  end
  if @parsed_body
    @event = @parsed_body[:event] || {}
    @target_id = @event[@target]
  end
  @auth = false
  init_vars
end

Instance Method Details

#decide_targetObject



27
28
29
30
31
32
33
34
35
# File 'lib/ezframe/editor.rb', line 27

def decide_target
  @target = self.class.to_s
  if @target.index("::")
    @target = $1 if /::(\w+)$/ =~ @target
  end
  @target.downcase!
  @target = @target.to_sym
  return @target
end

#init_varsObject



24
25
# File 'lib/ezframe/editor.rb', line 24

def init_vars
end

#make_detail_getObject



124
125
126
127
128
129
# File 'lib/ezframe/editor.rb', line 124

def make_detail_get
  layout = main_layout( left: sidenav, center: detail_table )
  @request.env['rack.session'][@target] = @target_id
  layout[:event] = "on=load:command=set_global@target=#{@target_id}"
  Materialize.convert(layout)
end

#make_index_table(data_a) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ezframe/editor.rb', line 86

def make_index_table(data_a)
  column_header = [:id, :name, :email, :zipcode, :prefecture]
  @column_set.each { |col| col.attribute.delete(:hidden) }
  a_element = Proc.new { |key, id, text|
    # mylog "proc executed"
    if key == :name
      Ht.a(href: "/#{@target}/detail?id=#{id}", child: text)
    else
      text
    end
  }
  table = PageKit::IndexTable.new(column_header: column_header, column_set: @column_set, add_checkbox: :id,
                                  decorate_column: a_element)
  table.make_table(data_a)
end

#public_create_getObject


add new parts



61
62
63
64
65
66
67
68
69
# File 'lib/ezframe/editor.rb', line 61

def public_create_get
  matrix = @column_set.map do |column|
    [column.label, column.form]
  end
  matrix.push([Ht.button(child: Message[:create_finish_button_label], class: %w[btn], event: "on=click:url=/#{@target}/create:with=form")])
  tb = Ht::Table.new(matrix)
  layout = main_layout(left: sidenav, center: Ht.form(child: tb.to_h))
  show_base_template(title: Message[:create_page_title], body: Html.convert(Materialize.convert(layout)))
end

#public_create_postObject



71
72
73
74
75
# File 'lib/ezframe/editor.rb', line 71

def public_create_post
  @column_set.values = @event[:form]
  @column_set[:id].value = @target_id = @column_set.create
  { redirect: "/#{@target}/detail?id=#{@target_id}" }
end

#public_detail_getObject


detail parts



116
117
118
119
120
121
122
# File 'lib/ezframe/editor.rb', line 116

def public_detail_get
  mylog "pubilc_detail_get: #{@request.params.inspect}"
  @target_id ||= @request.params["id"]
  data = @column_set.set_from_db(@target_id)
  return show_base_template(title: "no data", body: "no customer data: #{@target_id}") unless data
  show_base_template(title: Message[:index_page_title], body: Html.convert(make_detail_get))
end

#public_detail_postObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ezframe/editor.rb', line 131

def public_detail_post
  mylog "public_detail_post: #{@request.params.inspect}: #{@parsed_body}"
  if @parsed_body[:global]
    @target_id ||= @parsed_body[:global][@target]
  end
  @column_set.set_from_db(@target_id)
  case @event[:branch]
  when "update_value"
    update_value
  end
  { inject: "#center-panel", is_html: true, body: Html.convert(Materialize.convert(detail_table)) }
end

#public_index_getObject Also known as: public_default_get


index parts



79
80
81
82
83
84
# File 'lib/ezframe/editor.rb', line 79

def public_index_get
  data_a = @dataset.all
  htb = make_index_table(data_a)
  layout = index_layout(left: sidenav, center: Ht.form(child: htb))
  show_base_template(title: Message[:index_page_title], body: Html.convert(Materialize.convert(layout)))
end

#public_search_postObject


search parts



106
107
108
109
110
111
112
# File 'lib/ezframe/editor.rb', line 106

def public_search_post
  mylog "public_search_post: #{@parsed_body.inspect}"
  word = @event[:form][:word]
  pattern = "%#{word}%"
  data_a = @dataset.where(Sequel.|(Sequel.like(:name_kana, pattern), Sequel.like(:name, pattern))).all
  make_index_table(data_a)
end