Skip to content

Commit

Permalink
Generic attributes are editable
Browse files Browse the repository at this point in the history
Similar form to reference attribute editing.

Bumps #582
Enables #572
  • Loading branch information
lomky committed Jan 8, 2018
1 parent f3daf49 commit 0c53567
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/Tuba/DB/Mixin/Object/Generic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,37 @@ sub type {
return shift->attrs->{reftype};
}

sub set_attr {
my $self = shift;
my $args = shift;
my $new_attrs = $args->{new_attrs};
my $audit_user = $args->{audit_user};
my $audit_note = $args->{audit_note};
my $attr = $self->attrs;
foreach my $key ( keys %$new_attrs ) {
$attr->{ $key } = $new_attrs->{$key};
}
$self->attrs( $attr );
$self->save(audit_user => $audit_user, audit_note => $audit_note);
return 1;
}

sub delete_attr {
my $self = shift;
my $args = shift;
my $del_attr = $args->{del_attr};
my $audit_user = $args->{audit_user};
my $audit_note = $args->{audit_note};

my $attr = $self->attrs;
delete $attr->{ $del_attr };
$self->attrs( $attr );
$self->save(audit_user => $audit_user, audit_note => $audit_note);

return 1;
}



1;

46 changes: 46 additions & 0 deletions lib/Tuba/Generic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,51 @@ sub list {
$c->SUPER::list(@_);
}

sub update {
my $c = shift;
$c->SUPER::update(@_) if (my $json = $c->req->json);

my $generic = $c->_this_object or return $c->reply->not_found;

if (my $params = $c->req->params->to_hash ) {
if ( $params->{delete_pub_attr} ) {
$generic->delete_attr( { del_attr => $params->{delete_pub_attr}, audit_user => $c->user, audit_note => "Setting attributes" });
$c->redirect_without_error('update_form');
}
elsif ( exists $params->{new_attr_key} || /^attribute/ ~~ %$params ) {
my $new_attributes = _collect_attributes($params);
$generic->set_attr( { new_attrs => $new_attributes, audit_user => $c->user, audit_note => "Setting attributes" });
$c->redirect_without_error('update_form');
}
else {
$c->SUPER::update(@_);
}
}
}

sub _collect_attributes {
my ( $params ) = @_;
my $attrs;

# any newly entered key should overwrite existing of that name.
my $new_attr_flag = 0;
foreach my $key ( keys %$params ) {
next if $key eq 'new_attr_value';
if ( $key eq 'new_attr_key') {
$new_attr_flag = 1;
}
if ( $key =~ /^attribute_(.*)/ && $params->{ $key } ) {
$attrs->{"$1"} = $params->{$key};
}
}
if ( $new_attr_flag ) {
$attrs->{ $params->{'new_attr_key'} } = $params->{'new_attr_value'}
}

return $attrs;
}



1;

81 changes: 81 additions & 0 deletions lib/Tuba/files/templates/generic/update_form.html.ep
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
% layout 'default';
<center>
<h2><%= $object->meta->table %> : <%= $object->stringify %></h2>
</center>

% my $controls = stash 'controls' || {};

% my $textfield = begin
% my $column = shift;
% my $object = shift;
% my $accessor = $column->accessor_method_name;
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
<input type="text" class="form-control" id="<%= $column->name %>" name="<%= $column->name %>" placeholder="<%= $column->type %>" value="<%= $object->$accessor %>">
</div>
% end

% my $textarea = begin
% my $column = shift;
% my $object = shift;
% my $accessor = $column->accessor_method_name;
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
<textarea rows=7 class="form-control" id="<%= $column->name %>" name="<%= $column->name %>" placeholder="<%= $column->type %>"><%= $object->$accessor %></textarea>
</div>
% end

%= include 'update_tabs', active => 'fields';
<br />
<div class='tab-content'>
<p>To edit this generic publication, please choose a valid tab from above.</p>
</div>

%= hidden_field 'delete_pub_attr';
%= form_for obj_uri_for($object, 'update') => class => 'form-horizontal' => method => 'POST' => begin

<div class="well">
<table class='table table-responsive table-condensed table-bordered densetable'>
<caption>Attributes</caption>
<tr>
<th>Delete</th>
<th>Key</th>
<th>Current Value</th>
<th>Replace Value</th>
</tr>
% for my $k (sort keys %{ $object->attrs }) {
<tr>
<td><button type="submit" name='delete_pub_attr' value='<%= $k %>' class="btn btn-danger squeezevert" onclick='{return confirm("Are you sure you want to delete the attribute <%= $k %> (Value: <%= $object->attrs->{$k} %> )?")}'>Delete</button></td>
<td><%= $k %></td>
<td><%= $object->attrs->{$k} %></td>
<td><%= text_field 'attribute_'.$k, class => "form-control input-small squeezevert" %></td>
</tr>
% }
</table>
<h4>New Attribute</h4>
<div class='form-group row'>
<div class='col-lg-1 col-md-1 col-sm-1 control-label'>
%= label_for 'new_attr_key', class => 'required' => begin
Key
%= end
</div>
<div class='col-lg-2 col-md-2 col-sm-2'>
<%= text_field 'new_attr_key', class => "form-control input-small squeezevert" %>
</div>
<div class='col-lg-1 col-md-1 col-sm-1 control-label'>
%= label_for 'new_attr_value', class => 'required' => begin
Value
%= end
</div>
<div class='col-lg-4 col-md-4 col-sm-4'>
<%= text_field 'new_attr_value', class => "form-control input-small squeezevert" %>
</div>
</div> <!--- End new attr div --->

<div class="row">
<div class="col-lg-3 col-lg-offset-2 col-md-3 col-md-offset-2 col-sm-4 col-sm-offset-2">
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>

%= end

0 comments on commit 0c53567

Please sign in to comment.