rubyredmineredmine-plugins

Ruby / Redmine: compile a hash from issue subjects and appropriate custom fields


I’m using Redmine and Computed Custom Field plugin.

The plugin provides a possibility to make custom fields computed and it accepts ruby code for calculations.

In Redmine I have a project (Project_id = 11) where in I calculate the cost of products in a separate custom field for each issue. It looks like this:

The formula in the computed custom field (cf_id = 34) includes two hashes with prices of products (depending on the product type):

products_by_weight = {
    "Pears" => [110],
    "Tomatoes" => [120]
}

products_by_pieces = {
    "Pineapples" => [130,300],
    "Coconuts" => [140,200]
}

Then my formula checks the product selected in cf_id = 31 for belonging to the first or the second hashes and performs the corresponding calculations:

  1. Multiplies the price by weight (cf_id = 32) in case of using the goods from the first list
  2. Or multiplies the price by the quantity (cf_id = 33) in case of using the goods from the second list. The second value in the value array of "products_by_pieces" hash is the weight limit per piece. If the weight divided by the limit is a larger amount than entered in cf_id = 32, then the formula in scenario 2 will use this quantity instead of the one indicated in cf_id = 32.

Now I`m trying to move these variables outside the formula. I made a project (Project_id = 22) in which I want to save these variables as issues.

I imagine it like this:

I want to compile the same hashes that are now included in my formula in the cf_id = 34 of the issues of the project 11, but automatically from the issues of the project 22, taking into account the category.

So far, all I have achieved is to find the price of a known product from such issues of the project 22.

price = Project.find(22).issues.where(subject: "Pineapples").first.try(:custom_field_value,41)

But this does not help in any way and requires changes to the code when adding each new product.

I'm new to programming and Ruby, so I’m trying to experiment with Redmine classes, and tried to compile a hash with such code:

Issue.by_category(Project.find(22))

But as a result, so far I have received only this:

[{"status_id"=>"27", "closed"=>true, "category_id"=>"1", "total"=>"10"}]

Which is completely different from the result I expect. Any help would be helpful!

UPD. Right now, my variables (product prices and weight limits) are in a hash, which is directly a part of the code for computed field 34. But I do not want these variables (prices and weight limits) to be part of the code. I want to manage them as Issues with corresponding custom fields (41 and 42) in a separate project (22) - in such a way that regular user can change or add these values in Issues without having to change the code of the calculated custom field (34). So I want to compile that hash based on the Issues from Project 22 instead of writing it directly. I assume this is so, that the Subjects of the Issues of the Project 22 should become the keys and array of custom fields [41,41] - the values. In doing so, I need two separate hashes determined by the assigned category ("goods_by_weight" and "goods_by_pieces") because they are calculated differently and in Project 22 I have other variables written as values of custom fields in Issues with a different category.


Solution

  • I solved this problem in the following way.

    As planned, now I store the price list for my products as issues in a separate Project (ID 22). To get the prices hash of all products of the selected category (A) in the computed custom field's formula if Issue of Project (ID 11), I do the following:

    PRICELIST_PROJECT_ID = 22
    CATEGORY_A_ID = 1
    PRICE_VALUE_CFID = 41
    WLIMIT_VALUE_CFID = 42
    delimiter = ','
    
    pricelist_issues_cat_a = Project.find(PRICELIST_PROJECT_ID).issues.select { |rate| rate.category_id == CATEGORY_A_ID }
    cata_products_names = []
    cata_products_pvalues = []
    for i in (0..pricelist_issues_cat_a.size-1) do
      cata_products_names[i] = pricelist_issues_cat_a[i].try(:subject)
      cata_products_pvalues[i] = pricelist_issues_cat_a[i].try(:custom_field_value,PRICE_VALUE_CFID).split(Regexp.union(delimiter)).map(&:to_f)
    end
    
    cata_price_hash = Hash[cata_products_names.zip(cata_products_pvalues)]
    

    And the same way for product category B.

    Not sure if this is the most efficient way, but it works for me.