Skip to content

Commit

Permalink
Merge pull request ManageIQ#1668 from mkanoor/expression_method_ui_ch…
Browse files Browse the repository at this point in the history
…anges

Expression method ui changes
  • Loading branch information
Dan Clarizio authored Aug 15, 2017
2 parents 580e25b + f03c8c2 commit af7bdaa
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 10 deletions.
10 changes: 7 additions & 3 deletions app/controllers/application_controller/advanced_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ def adv_search_build(model)
adv_search_clear_default_search_if_cant_be_seen
@edit.delete(:exp_token) # Remove any existing atom being edited
else # Create new exp fields
@edit = {}
@edit ||= {}
@edit[@expkey] ||= ApplicationController::Filter::Expression.new
@edit[@expkey][:expression] = {"???" => "???"} # Set as new exp element
@edit[@expkey][:use_mytags] = true # Include mytags in tag search atoms
@edit[:custom_search] = false # setting default to false
@edit[:new] = {}
@edit[:new][@expkey] = @edit[@expkey][:expression] # Copy to new exp
@edit[:new] ||= {}
if @edit[:new][@expkey]
@edit[@expkey][:expression] = @edit[:new][@expkey] # Copy to new exp
else
@edit[:new][@expkey] = @edit[@expkey][:expression] # Copy to new exp
end
@edit[@expkey].history.reset(@edit[@expkey][:expression])
@edit[:adv_search_open] = false
@edit[@expkey][:exp_model] = model.to_s
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/application_controller/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def exp_button
else
page.replace("exp_editor_div", :partial => "layouts/exp_editor")
end

if @edit[:expression_method]
page.replace("exp_editor_div", :partial => "layouts/exp_editor")
end

if ["not", "discard", "commit", "remove"].include?(params[:pressed])
page << javascript_hide("exp_buttons_on")
page << javascript_hide("exp_buttons_not")
Expand Down
67 changes: 66 additions & 1 deletion app/controllers/miq_ae_class_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,17 @@ def set_method_form_vars
@edit[:new][:scope] = "instance"
@edit[:new][:language] = "ruby"
@edit[:new][:available_locations] = MiqAeMethod.available_locations
@edit[:new][:available_expression_objects] = MiqAeMethod.available_expression_objects.sort
@edit[:new][:location] = @ae_method.location.nil? ? "inline" : @ae_method.location
if @edit[:new][:location] == "expression"
expr_hash = YAML.load(@ae_method.data)
if expr_hash[:db] && expr_hash[:expression]
@edit[:new][:expression] = expr_hash[:expression]
expression_setup(expr_hash[:db])
end
else
@edit[:new][:data] = @ae_method.data.to_s
end
@edit[:new][:data] = @ae_method.data.to_s
if @edit[:new][:location] == "inline" && !@ae_method.data
@edit[:new][:data] = MiqAeMethod.default_method_text
Expand All @@ -746,6 +756,20 @@ def set_method_form_vars
session[:changed] = @changed = false
end

def expression_setup(db)
@edit[:expression_method] = true
@edit[:new][:exp_object] = db
if params[:exp_object] || params[:cls_exp_object]
session[:adv_search] = nil
@edit[@expkey] = @edit[:new][@expkey] = nil
end
adv_search_build(db)
end

def expression_cleanup
@edit[:expression_method] = false
end

def ae_class_for_instance_or_method(record)
record.id ? record.ae_class : MiqAeClass.find_by_id(from_cid(x_node.split("-").last))
end
Expand Down Expand Up @@ -844,6 +868,14 @@ def form_method_field_changed
return unless load_edit("aemethod_edit__#{params[:id]}", "replace_cell__explorer")
@prev_location = @edit[:new][:location]
get_method_form_vars

if @edit[:new][:location] == 'expression'
@edit[:new][:exp_object] ||= @edit[:new][:available_expression_objects].first
exp_object = params[:cls_exp_object] || params[:exp_object] || @edit[:new][:exp_object]
expression_setup(exp_object) if exp_object
else
expression_cleanup
end
if row_selected_in_grid?
@refresh_div = "class_methods_div"
@refresh_partial = "class_methods"
Expand All @@ -869,7 +901,8 @@ def form_method_field_changed
@edit[:default_verify_status] = @edit[:new][:location] == "inline" && @edit[:new][:data] && @edit[:new][:data] != ""
render :update do |page|
page << javascript_prologue
page.replace_html(@refresh_div, :partial => @refresh_partial) if @refresh_div && @prev_location != @edit[:new][:location]
page.replace_html('form_div', :partial => 'method_form', :locals => {:prefix => ""}) if @edit[:new][:location] == 'expression'
page.replace_html(@refresh_div, :partial => @refresh_partial) if @refresh_div && (@prev_location != @edit[:new][:location] || params[:exp_object] || params[:cls_exp_object])
# page.replace_html("hider_1", :partial=>"method_data", :locals=>{:field_name=>@field_name}) if @prev_location != @edit[:new][:location]
if params[:cls_field_datatype]
if session[:field_data][:datatype] == "password"
Expand Down Expand Up @@ -927,6 +960,7 @@ def form_method_field_changed
end
end
page << javascript_for_miq_button_visibility_changed(@changed)
page << "miqSparkle(false)"
end
end
end
Expand Down Expand Up @@ -1082,6 +1116,10 @@ def update_method
@in_a_form = false
replace_right_cell
when "save"
# dont allow save if expression has not been added or existing one has been removed
validate_expression("save") if @edit[:new][:location] == 'expression'
return if flash_errors?

ae_method = find_record_with_rbac(MiqAeMethod, params[:id])
set_method_record_vars(ae_method) # Set the record variables, but don't save
begin
Expand Down Expand Up @@ -1177,6 +1215,11 @@ def create
end
end

def data_for_expression
{:db => @edit[:new][:exp_object],
:expression => @edit[:new][:expression]}.to_yaml
end

def create_method
assert_privileges("miq_ae_method_new")
@in_a_form = true
Expand All @@ -1189,6 +1232,11 @@ def create_method
when "add"
return unless load_edit("aemethod_edit__new", "replace_cell__explorer")
get_method_form_vars

# dont allow add if expression has not been added or existing one has been removed
validate_expression("add") if @edit[:new][:location] == 'expression'
return if flash_errors?

add_aemethod = MiqAeMethod.new
set_method_record_vars(add_aemethod) # Set the record variables, but don't save
begin
Expand Down Expand Up @@ -1571,6 +1619,14 @@ def refresh_git_domain

private

def validate_expression(task)
if @edit[@expkey][:expression]["???"] == "???"
add_flash(_("Error during '%{task}': Expression element is required") % {:task => _(task)}, :error)
@in_a_form = true
javascript_flash
end
end

def features
[ApplicationController::Feature.new_with_hash(:role => "miq_ae_class_explorer",
:role_any => true,
Expand Down Expand Up @@ -2147,6 +2203,11 @@ def set_method_record_vars(miqaemethod)
miqaemethod.location = @edit[:new][:location]
miqaemethod.language = @edit[:new][:language]
miqaemethod.data = @edit[:new][:data]
miqaemethod.data = if @edit[:new][:location] == 'expression'
data_for_expression
else
@edit[:new][:data]
end
miqaemethod.class_id = from_cid(@edit[:ae_class_id])
end

Expand Down Expand Up @@ -2502,6 +2563,10 @@ def get_method_node_info(id)
inputs = @record.inputs
@sb[:squash_state] = true
@sb[:active_tab] = "methods"
if @record.location == 'expression'
hash = YAML.load(@record.data)
@expression = hash[:expression] ? MiqExpression.new(hash[:expression]).to_human : ""
end
domain_overrides
set_right_cell_text(x_node, @record)
end
Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ def clear_search_status
QS_VALID_USER_INPUT_OPERATORS = ["=", "!=", ">", ">=", "<", "<=", "INCLUDES", "STARTS WITH", "ENDS WITH", "CONTAINS"]
QS_VALID_FIELD_TYPES = [:string, :boolean, :integer, :float, :percent, :bytes, :megabytes]
def qs_show_user_input_checkbox?
return true if @edit[:expression_method]
return false unless @edit[:adv_search_open] # Only allow user input for advanced searches
return false unless QS_VALID_USER_INPUT_OPERATORS.include?(@edit[@expkey][:exp_key])
val = (@edit[@expkey][:exp_typ] == "field" && # Field atoms with certain field types return true
Expand Down
31 changes: 26 additions & 5 deletions app/views/miq_ae_class/_method_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
:maxlength => ViewHelper::MAX_NAME_LEN,
:class => "form-control",
"data-miq_observe" => obs)

.form-group
%label.col-md-2.control-label
= _('Location')
Expand All @@ -39,17 +40,37 @@
"data-miq_observe" => {:url => url}.to_json)
:javascript
miqInitSelectPicker();
miqSelectPickerEvent("#{prefix}method_location", "#{url}")
miqSelectPickerEvent("#{prefix}method_location", "#{url}", {beforeSend: true})

- if @ae_method.created_on
.form-group
%label.col-md-2.control-label
= _("Created On")
.col-md-8
= h(format_timezone(@ae_method.created_on, Time.zone, "gtl"))
%hr
%h3= (@edit[:new][:location] == 'builtin') ? _('Builtin name') : _("Data")
= render :partial => "method_data", :locals => {:field_name => "#{prefix}method"}
%hr

- if @edit[:new][:location] == 'expression'
.form-group
%label.col-md-2.control-label
= _('Expression Object')
.col-md-8
= select_tag("#{prefix}exp_object",
options_for_select(@edit[:new][:available_expression_objects],
@edit[:new][:exp_object]),
:class => "selectpicker",
"data-miq_observe" => {:url => url}.to_json)
:javascript
miqInitSelectPicker();
miqSelectPickerEvent("#{prefix}exp_object", "#{url}", {beforeSend: true})
- else
%hr
%h3= @edit[:new][:location] == 'builtin' ? _('Builtin name') : _("Data")
= render :partial => "method_data", :locals => {:field_name => "#{prefix}method"}
%hr
- if @edit[:new][:location] == 'expression'
.div
%h3= _("Expression (Choose an element of the expression to edit)")
= render :partial => 'layouts/exp_editor'
%h3= _("Input Parameters")
%table.table.table-bordered.table-striped
%thead
Expand Down
2 changes: 2 additions & 0 deletions app/views/miq_ae_class/_method_inputs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
:mode => "ruby",
:line_numbers => true,
:read_only => true}
- elsif @ae_method.location == 'expression'
= @expression
- else
= @ae_method.data
-# show inputs parameters grid if there are any inputs
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,8 @@
x_button
x_history
x_show
)
) + adv_search_post +
exp_post
},
:miq_ae_customization => {
:get => %w(
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/miq_ae_class_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@
context "when the record exists" do
before do
allow(MiqAeMethod).to receive(:find_by_id).with(123).and_return(miq_ae_method)
allow(miq_ae_method).to receive(:location).with(no_args).and_return("inline")
allow(miq_ae_method).to receive(:ae_class).and_return(miq_ae_class)
allow(MiqAeMethod).to receive(:get_homonymic_across_domains)
.with(@user, "fqname").and_return([override, override2])
Expand Down

0 comments on commit af7bdaa

Please sign in to comment.