Module: Ezframe::MainPageKit::Edit

Included in:
Ezframe::MainEditor
Defined in:
lib/ezframe/main_page_kit.rb

Instance Method Summary collapse

Instance Method Details

#count_errors(validate_result) ⇒ Object

validate_resultの中のエラーの数を数える



170
171
172
# File 'lib/ezframe/main_page_kit.rb', line 170

def count_errors(validate_result)
  return validate_result.count { |k, a| a[1] }
end

#edit_cancel_buttonObject



174
175
176
# File 'lib/ezframe/main_page_kit.rb', line 174

def edit_cancel_button
  Ht.span(class: %w[btn red small waves-effect waves-light switch-button], child: Ht.icon("clear"))
end

#exec_completionObject

自動入力を行う



104
105
106
# File 'lib/ezframe/main_page_kit.rb', line 104

def exec_completion
  return nil
end

#full_validation(validate_h) ⇒ Object

全てのカラムに対してバリデーションを行う



145
146
147
148
149
150
# File 'lib/ezframe/main_page_kit.rb', line 145

def full_validation(validate_h)
  cmd_a = show_validate_result(validate_h)
  cmd_a.unshift({ reset_error: ".error-box" })
  EzLog.debug("full_validation: full, cmd_a=#{cmd_a}")
  return cmd_a
end

#make_create_button(event = nil) ⇒ Object



178
179
180
181
# File 'lib/ezframe/main_page_kit.rb', line 178

def make_create_button(event = nil)
  event ||= "on=click:command=redirect:url=#{make_base_url}/create"
  return Ht.button(class: %w[btn], child: [Ht.icon("add"), Message[:create_button_label]], ezevent: event)
end

#make_edit_form(command = :edit) ⇒ Object

編集フォームの生成



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ezframe/main_page_kit.rb', line 109

def make_edit_form(command = :edit)
  table = []
  matrix = @column_set.map do |column|
    next if column.no_edit?
    form = column.form
    table.push Ht.p([Ht.small(column.label), form]) if form
  end
  send_button = Ht.button(id: "edit-finish-button", child: Message[:edit_finish_button_label], class: %w[btn], ezevent: "on=click:url=#{make_base_url}/#{command}:with=form")
  cancel_button = edit_cancel_button
  cancel_button[:event] = "on=click:command=redirect:url=#{make_base_url}"
  table.push(Ht.p([send_button, cancel_button]))
  return table
end

#public_create_getObject

新規登録フォーム表示



49
50
51
52
53
54
# File 'lib/ezframe/main_page_kit.rb', line 49

def public_create_get
  @column_set.clear
  table = make_edit_form(:create)
  layout = main_layout(center: make_form("#{make_base_url}/create", table), type: 2)
  show_base_template(title: Message[:parent_create_page_title], body: Html.convert(layout))
end

#public_create_postObject

新規登録受信



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ezframe/main_page_kit.rb', line 57

def public_create_post
  validation = @column_set.validate(@form)
  EzLog.debug("public_create_post: event=#{@event}, form=#{@form}")
  if @event[:branch] == "single_validate"
    EzLog.debug("public_create_post: single validate")
    return single_validation(validation, @event[:target_key] || @form.keys[0])
  end
  unless @form
    url = "#{make_base_url}/create"
    return { inject: "#center-panel", body: Html.convert(make_form(url, make_edit_form(:create))), set_url: url }
  else
    if count_errors(validation) > 0
      cmd_a = full_validation(validation)
      EzLog.debug("public_create_post: cmd_a=#{cmd_a}")
      return cmd_a if cmd_a.length > 0
    end
    # 値の保存
    id = create_data(@form)
    return { redirect: make_base_url(id) }
  end
end

#public_edit_postObject

データ編集受信



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ezframe/main_page_kit.rb', line 80

def public_edit_post
  EzLog.debug("public_edit_post: #{@form}")
  @id = get_id
  validation = @column_set.validate(@form)
  if @event[:branch] == "single_validate"
    EzLog.debug("public_edit_post: single validate:event=#{@event}, form=#{@form}")
    return single_validation(validation, @event[:target_key])
  end
  unless @form
    data = @column_set.set_from_db(@id)
    return show_message_page("no data", "data is not defined: #{@id}") unless data
    return { inject: "#center-panel", body: Html.convert(make_form("#{make_base_url}/edit", make_edit_form)) }
  else
    if count_errors(validation) > 0
      cmd_a = full_validation(validation)
      return cmd_a
    end
    # 値を保存
    update_data(@id, @form)
    return { redirect: make_base_url(@id) }
  end
end

#show_validate_result(validate_result) ⇒ Object

フォームの値の有効性チェックし、ajax用返信ハッシュを生成



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ezframe/main_page_kit.rb', line 153

def show_validate_result(validate_result)
  cmd_a = []
  validate_result.each do |key, status|
    norm, err = status
    EzLog.debug("norm=#{norm}, err=#{err}")
    if norm
      cmd_a.push({ set_value: "input[name=#{key}]", value: norm })
    end
    if err
      msg = Message[err.to_sym] || err
      cmd_a.push({ set_error: "#error-box-#{key}", value: msg })
    end
  end
  return cmd_a
end

#single_validation(validate_h, target_key) ⇒ Object

1カラムに対してだけバリデーションを行う。



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ezframe/main_page_kit.rb', line 124

def single_validation(validate_h, target_key)
  EzLog.debug("single_validation: validate_h=#{validate_h}, target_key=#{target_key}")
  unless target_key
    raise "target_key is empty: #{validate_h}"
    return []
  end
  cmd_a = []
  if validate_h[target_key.to_sym]
    cmd_a = show_validate_result(validate_h)
  end
  if count_errors(validate_h) == 0
    cmd_a.unshift({ reset_error: "#error-box-#{target_key}" })
  end
  comp_a = exec_completion(@form)
  cmd_a += comp_a if comp_a
  EzLog.debug("reset_error: #error-box-#{target_key}")
  EzLog.debug("single_validation: target_key=#{target_key}, validate_h=#{validate_h}, count=#{count_errors(validate_h)}, cmd_a=#{cmd_a}")
  return cmd_a
end