Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Proposal: find_attribute_value User.pm #16

Closed
sbatschelet opened this Issue · 0 comments

1 participant

@sbatschelet
Collaborator

Quick method to return an attribute value. This is a rough draft tests etc would need to be done.

Usage

# with current logged in user
find_attribute_value(hair_color);

# with defined user_id
find_attribute_value(hair_color,3);

sub find_attribute_value {
    my ($self, $attr, $user_id) = @_;

    # attribute must be set
    unless ($attr) {
              return (0, "Input requires attribute");
    };
    my $attr_rs = $self->result_source->schema->resultset('Attribute');
    my $attribute = $attr_rs->find({ name => $attr });

    # attribute must exist
    unless ($attribute) {
              return (0, "Attribute does not exist.");
    };

    # if user_id is not passed the lets use the current user
    if (!$user_id) {
        $user_id = $self->id;
    }
    my $user_rs = $self->result_source->schema->resultset('User');
    my $user = $user_rs->find( $user_id );

    # find records
    my $user_attribute = $user->find_related('UserAttribute',
                                            {attributes_id => $attribute->id});

    my $user_attribute_value = $user_attribute->find_related('UserAttributeValue',
                                            {user_attributes_id => $user_attribute->id});

    return $user_attribute_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.