Added quantity filtering by domain
This commit is contained in:
parent
d3ba8579d6
commit
2ebbe9a306
@ -1,6 +1,7 @@
|
|||||||
class IngredientsController < ApplicationController
|
class IngredientsController < ApplicationController
|
||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
|
before_action :init_session_filters
|
||||||
before_action :find_project_by_project_id,
|
before_action :find_project_by_project_id,
|
||||||
only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients]
|
only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients]
|
||||||
before_action :find_quantity, only: [:toggle_nutrient_column]
|
before_action :find_quantity, only: [:toggle_nutrient_column]
|
||||||
@ -54,13 +55,13 @@ class IngredientsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def filter
|
def filter
|
||||||
session[:filters] = params[:filters]
|
session[:i_filters] = params[:filters]
|
||||||
prepare_ingredients
|
prepare_ingredients
|
||||||
render :toggle
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_nutrients
|
def filter_nutrients
|
||||||
session[:filters] = params[:filters]
|
session[:i_filters] = params[:filters]
|
||||||
prepare_nutrients
|
prepare_nutrients
|
||||||
render :toggle_nutrient_column
|
render :toggle_nutrient_column
|
||||||
end
|
end
|
||||||
@ -167,6 +168,10 @@ class IngredientsController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def init_session_filters
|
||||||
|
session[:i_filters] ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
def ingredient_params
|
def ingredient_params
|
||||||
params.require(:ingredient).permit(
|
params.require(:ingredient).permit(
|
||||||
:name,
|
:name,
|
||||||
@ -197,13 +202,13 @@ class IngredientsController < ApplicationController
|
|||||||
|
|
||||||
def prepare_ingredients
|
def prepare_ingredients
|
||||||
@ingredients, @formula_q = @project.ingredients.includes(:ref_unit, :source)
|
@ingredients, @formula_q = @project.ingredients.includes(:ref_unit, :source)
|
||||||
.filter(@project, session[:filters])
|
.filter(@project, session[:i_filters])
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare_nutrients
|
def prepare_nutrients
|
||||||
@quantities = @project.quantities.where(primary: true)
|
@quantities = @project.quantities.where(primary: true)
|
||||||
ingredients, requested_n, extra_n, @formula_q = @project.ingredients
|
ingredients, requested_n, extra_n, @formula_q = @project.ingredients
|
||||||
.filter(@project, session[:filters], @quantities)
|
.filter(@project, session[:i_filters], @quantities)
|
||||||
|
|
||||||
@nutrients = {}
|
@nutrients = {}
|
||||||
@extra_nutrients = {}
|
@extra_nutrients = {}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
class QuantitiesController < ApplicationController
|
class QuantitiesController < ApplicationController
|
||||||
before_action :find_project_by_project_id, only: [:index, :create]
|
before_action :init_session_filters
|
||||||
|
before_action :find_project_by_project_id, only: [:index, :create, :filter]
|
||||||
before_action :find_quantity, only: [:destroy, :toggle, :up, :down, :left, :right]
|
before_action :find_quantity, only: [:destroy, :toggle, :up, :down, :left, :right]
|
||||||
before_action :authorize
|
before_action :authorize
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@quantity = @project.quantities.new
|
@quantity = @project.quantities.new
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@ -14,7 +15,7 @@ class QuantitiesController < ApplicationController
|
|||||||
flash[:notice] = 'Created new quantity'
|
flash[:notice] = 'Created new quantity'
|
||||||
redirect_to project_quantities_url(@project)
|
redirect_to project_quantities_url(@project)
|
||||||
else
|
else
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
render :index
|
render :index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -23,41 +24,51 @@ class QuantitiesController < ApplicationController
|
|||||||
if @quantity.destroy
|
if @quantity.destroy
|
||||||
flash[:notice] = 'Deleted quantity'
|
flash[:notice] = 'Deleted quantity'
|
||||||
end
|
end
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
render :toggle
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle
|
def toggle
|
||||||
@quantity.toggle_primary!
|
@quantity.toggle_primary!
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter
|
||||||
|
session[:q_filters] = params[:filters]
|
||||||
|
prepare_quantities
|
||||||
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
def up
|
def up
|
||||||
@quantity.move_left if @quantity.left_sibling.present?
|
@quantity.move_left if @quantity.left_sibling.present?
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
render :toggle
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
def down
|
def down
|
||||||
@quantity.move_right if @quantity.right_sibling.present?
|
@quantity.move_right if @quantity.right_sibling.present?
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
render :toggle
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
def left
|
def left
|
||||||
@quantity.move_to_right_of(@quantity.parent) if @quantity.parent.present?
|
@quantity.move_to_right_of(@quantity.parent) if @quantity.parent.present?
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
render :toggle
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
def right
|
def right
|
||||||
@quantity.move_to_child_of(@quantity.left_sibling) if @quantity.left_sibling.present?
|
@quantity.move_to_child_of(@quantity.left_sibling) if @quantity.left_sibling.present?
|
||||||
@quantities = @project.quantities
|
prepare_quantities
|
||||||
render :toggle
|
render :toggle
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def init_session_filters
|
||||||
|
session[:q_filters] ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
def quantity_params
|
def quantity_params
|
||||||
params[:quantity].delete(:formula) if params[:quantity][:formula].blank?
|
params[:quantity].delete(:formula) if params[:quantity][:formula].blank?
|
||||||
params.require(:quantity).permit(
|
params.require(:quantity).permit(
|
||||||
@ -69,4 +80,8 @@ class QuantitiesController < ApplicationController
|
|||||||
:primary
|
:primary
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prepare_quantities
|
||||||
|
@quantities = @project.quantities.filter(@project, session[:q_filters])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -15,7 +15,7 @@ module IngredientsHelper
|
|||||||
end
|
end
|
||||||
|
|
||||||
def visibility_options(selected)
|
def visibility_options(selected)
|
||||||
options = [["all", nil], ["visible", 1], ["hidden", 0]]
|
options = [["visible", 1], ["hidden", 0]]
|
||||||
options_for_select(options, selected)
|
options_for_select(options, selected)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
module QuantitiesHelper
|
module QuantitiesHelper
|
||||||
def domain_options
|
def domain_options
|
||||||
translations = t('.domains')
|
translations = t('quantities.form.domains')
|
||||||
Quantity.domains.map do |k,v|
|
Quantity.domains.map do |k,v|
|
||||||
[translations[k.to_sym], k]
|
[translations[k.to_sym], k]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def domain_options_tag(selected)
|
||||||
|
options_for_select(domain_options, selected)
|
||||||
|
end
|
||||||
|
|
||||||
def parent_options(domain)
|
def parent_options(domain)
|
||||||
options = nested_set_options(@quantities.send(domain), @quantity) do |i|
|
options = nested_set_options(@quantities.send(domain), @quantity) do |i|
|
||||||
raw("#{' ' * i.level}#{i.name}")
|
raw("#{' ' * i.level}#{i.name}")
|
||||||
|
@ -41,7 +41,7 @@ class Ingredient < ActiveRecord::Base
|
|||||||
self.toggle!(:hidden)
|
self.toggle!(:hidden)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.filter(project, filters = {}, requested_q = Quantity.none)
|
def self.filter(project, filters, requested_q = Quantity.none)
|
||||||
ingredients = all
|
ingredients = all
|
||||||
|
|
||||||
if filters[:name].present?
|
if filters[:name].present?
|
||||||
|
@ -34,4 +34,14 @@ class Quantity < ActiveRecord::Base
|
|||||||
def calculate(inputs)
|
def calculate(inputs)
|
||||||
Formula.new(self.project, self.formula).calculate(inputs)
|
Formula.new(self.project, self.formula).calculate(inputs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.filter(project, filters)
|
||||||
|
quantities = all
|
||||||
|
|
||||||
|
if filters[:domain].present?
|
||||||
|
quantities = quantities.where(domain: domains[filters[:domain]])
|
||||||
|
end
|
||||||
|
|
||||||
|
quantities
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -7,27 +7,28 @@
|
|||||||
<table class="filter">
|
<table class="filter">
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<%= text_field_tag 'filters[name]', session[:filters][:name], placeholder: 'name',
|
<%= text_field_tag 'filters[name]', session[:i_filters][:name], placeholder: 'name',
|
||||||
:onblur => '$("#filters_form").submit(); return false;' %>
|
onblur: '$("#filters_form").submit(); return false;' %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= select_tag 'filters[visibility]',
|
<%= select_tag 'filters[visibility]',
|
||||||
visibility_options(session[:filters][:visibility]),
|
visibility_options(session[:i_filters][:visibility]),
|
||||||
:onchange => '$("#filters_form").submit(); return false;' %>
|
prompt: t('.visibility_prompt'),
|
||||||
|
onchange: '$("#filters_form").submit(); return false;' %>
|
||||||
</td>
|
</td>
|
||||||
<td style="width:100%;">
|
<td style="width:100%;">
|
||||||
<%= text_field_tag 'filters[nutrients]', session[:filters][:nutrients],
|
<%= text_field_tag 'filters[nutrients]', session[:i_filters][:nutrients],
|
||||||
placeholder: 'conditional expression including nutrients', size: 40,
|
placeholder: 'conditional expression including nutrients', size: 40,
|
||||||
:style => 'box-sizing:border-box; width:100%;',
|
style: 'box-sizing:border-box; width:100%;',
|
||||||
:onblur => '$("#filters_form").submit(); return false;' %>
|
onblur: '$("#filters_form").submit(); return false;' %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= link_to l(:button_apply), '#', :class => "icon icon-checked",
|
<%= link_to l(:button_apply), '#', class: "icon icon-checked",
|
||||||
:onclick => '$("#filters_form").submit(); return false;' %>
|
onclick: '$("#filters_form").submit(); return false;' %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= link_to l(:button_clear), '#', :class => "icon icon-reload",
|
<%= link_to l(:button_clear), '#', class: "icon icon-reload",
|
||||||
:onclick => '$("#filters_form input, #filters_form select").val("");
|
onclick: '$("#filters_form input, #filters_form select").val("");
|
||||||
$("#filters_form").submit(); return false;' %>
|
$("#filters_form").submit(); return false;' %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
27
app/views/quantities/_filters.html.erb
Normal file
27
app/views/quantities/_filters.html.erb
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<fieldset id="filters" class="collapsible">
|
||||||
|
<legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend>
|
||||||
|
<div>
|
||||||
|
<%= form_tag url, id: 'filters_form', method: :get, remote: true do %>
|
||||||
|
<table class="filter">
|
||||||
|
<tr>
|
||||||
|
<td style="width:100%;">
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= select_tag 'filters[domain]', domain_options_tag(session[:q_filters][:domain]),
|
||||||
|
prompt: t('.domain_prompt'),
|
||||||
|
onchange: '$("#filters_form").submit(); return false;' %>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= link_to l(:button_apply), '#', class: "icon icon-checked",
|
||||||
|
onclick: '$("#filters_form").submit(); return false;' %>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<%= link_to l(:button_clear), '#', class: "icon icon-reload",
|
||||||
|
onclick: '$("#filters_form input, #filters_form select").val("");
|
||||||
|
$("#filters_form").submit(); return false;' %>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
@ -1,3 +1,6 @@
|
|||||||
|
<%= render :partial => 'quantities/filters',
|
||||||
|
:locals => {:url => filter_project_quantities_path(@project)} %>
|
||||||
|
|
||||||
<% if @quantities.any? { |q| q.persisted? } %>
|
<% if @quantities.any? { |q| q.persisted? } %>
|
||||||
<table class="list">
|
<table class="list">
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -67,6 +67,8 @@ en:
|
|||||||
contextual:
|
contextual:
|
||||||
link_import_ingredients: 'Import'
|
link_import_ingredients: 'Import'
|
||||||
link_new_ingredient: 'New ingredient'
|
link_new_ingredient: 'New ingredient'
|
||||||
|
filters:
|
||||||
|
visibility_prompt: 'all'
|
||||||
import:
|
import:
|
||||||
heading_import_ingredients: 'Import'
|
heading_import_ingredients: 'Import'
|
||||||
label_import_select_csv_file: 'Select CSV file'
|
label_import_select_csv_file: 'Select CSV file'
|
||||||
@ -98,6 +100,8 @@ en:
|
|||||||
heading_new_source: 'New source'
|
heading_new_source: 'New source'
|
||||||
link_new_source: 'New source'
|
link_new_source: 'New source'
|
||||||
quantities:
|
quantities:
|
||||||
|
filters:
|
||||||
|
domain_prompt: 'all'
|
||||||
index:
|
index:
|
||||||
heading: 'Quantities'
|
heading: 'Quantities'
|
||||||
heading_new_quantity: 'New quantity'
|
heading_new_quantity: 'New quantity'
|
||||||
|
@ -21,6 +21,7 @@ resources :projects do
|
|||||||
resources :quantities, :only => [:index, :create, :destroy] do
|
resources :quantities, :only => [:index, :create, :destroy] do
|
||||||
post 'toggle', on: :member
|
post 'toggle', on: :member
|
||||||
post 'up', 'down', 'left', 'right', on: :member
|
post 'up', 'down', 'left', 'right', on: :member
|
||||||
|
get 'filter', on: :collection
|
||||||
end
|
end
|
||||||
resources :units, :only => [:index, :create, :destroy]
|
resources :units, :only => [:index, :create, :destroy]
|
||||||
end
|
end
|
||||||
|
2
init.rb
2
init.rb
@ -19,7 +19,7 @@ Redmine::Plugin.register :body_tracking do
|
|||||||
:measurements => [:index],
|
:measurements => [:index],
|
||||||
:ingredients => [:index, :nutrients, :filter, :filter_nutrients],
|
:ingredients => [:index, :nutrients, :filter, :filter_nutrients],
|
||||||
:sources => [:index],
|
:sources => [:index],
|
||||||
:quantities => [:index],
|
:quantities => [:index, :filter],
|
||||||
:units => [:index],
|
:units => [:index],
|
||||||
}, read: true
|
}, read: true
|
||||||
permission :manage_common, {
|
permission :manage_common, {
|
||||||
|
Reference in New Issue
Block a user