Filtering ingredients by name
Nutrients link in sidebar
This commit is contained in:
parent
cf9c913897
commit
1acb72f33d
@ -1,9 +1,10 @@
|
|||||||
class IngredientsController < ApplicationController
|
class IngredientsController < ApplicationController
|
||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients]
|
before_action :find_project_by_project_id,
|
||||||
before_action :find_ingredient, only: [:destroy, :toggle]
|
only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients]
|
||||||
before_action :find_quantity, only: [:toggle_nutrient_column]
|
before_action :find_quantity, only: [:toggle_nutrient_column]
|
||||||
|
before_action :find_ingredient, only: [:destroy, :toggle]
|
||||||
before_action :authorize
|
before_action :authorize
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@ -11,7 +12,7 @@ class IngredientsController < ApplicationController
|
|||||||
# passing attr for Nutrient after_initialize
|
# passing attr for Nutrient after_initialize
|
||||||
@ingredient.nutrients.new(ingredient: @ingredient)
|
@ingredient.nutrients.new(ingredient: @ingredient)
|
||||||
|
|
||||||
@ingredients = @project.ingredients.includes(:ref_unit, :source)
|
prepare_ingredients
|
||||||
@ingredients << @ingredient
|
@ingredients << @ingredient
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -27,18 +28,20 @@ class IngredientsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
# FIXME: redirect to :back?
|
||||||
@ingredient = @project.ingredients.new(ingredient_params)
|
@ingredient = @project.ingredients.new(ingredient_params)
|
||||||
if @ingredient.save
|
if @ingredient.save
|
||||||
flash[:notice] = 'Created new ingredient'
|
flash[:notice] = 'Created new ingredient'
|
||||||
redirect_to project_ingredients_url(@project)
|
redirect_to project_ingredients_url(@project)
|
||||||
else
|
else
|
||||||
@ingredients = @project.ingredients.includes(:ref_unit, :source)
|
prepare_ingredients
|
||||||
@ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty?
|
@ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty?
|
||||||
render :index
|
render :index
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
# FIXME: redirect to :back?
|
||||||
# FIXME: don't destroy if any meal depend on ingredient
|
# FIXME: don't destroy if any meal depend on ingredient
|
||||||
if @ingredient.destroy
|
if @ingredient.destroy
|
||||||
flash[:notice] = 'Deleted ingredient'
|
flash[:notice] = 'Deleted ingredient'
|
||||||
@ -48,10 +51,23 @@ class IngredientsController < ApplicationController
|
|||||||
|
|
||||||
def toggle
|
def toggle
|
||||||
@ingredient.update(hidden: !@ingredient.hidden)
|
@ingredient.update(hidden: !@ingredient.hidden)
|
||||||
@ingredients = @project.ingredients.includes(:ref_unit, :source)
|
prepare_ingredients
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter
|
||||||
|
session[:filters] = params[:filters]
|
||||||
|
prepare_ingredients
|
||||||
|
render :toggle
|
||||||
|
end
|
||||||
|
|
||||||
|
def filter_nutrients
|
||||||
|
session[:filters] = params[:filters]
|
||||||
|
prepare_nutrients
|
||||||
|
render :toggle_nutrient_column
|
||||||
end
|
end
|
||||||
|
|
||||||
def import
|
def import
|
||||||
|
# FIXME: redirect to :back?
|
||||||
warnings = []
|
warnings = []
|
||||||
|
|
||||||
if params.has_key?(:file)
|
if params.has_key?(:file)
|
||||||
@ -181,8 +197,13 @@ class IngredientsController < ApplicationController
|
|||||||
render_404
|
render_404
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prepare_ingredients
|
||||||
|
@ingredients = filter_ingredients(@project.ingredients.includes(:ref_unit, :source))
|
||||||
|
end
|
||||||
|
|
||||||
def prepare_nutrients
|
def prepare_nutrients
|
||||||
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
|
ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit])
|
||||||
|
ingredients = filter_ingredients(ingredients)
|
||||||
@primary_quantities = @project.quantities.where(primary: true)
|
@primary_quantities = @project.quantities.where(primary: true)
|
||||||
@primary_nutrients = {}
|
@primary_nutrients = {}
|
||||||
@extra_nutrients = {}
|
@extra_nutrients = {}
|
||||||
@ -198,4 +219,12 @@ class IngredientsController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def filter_ingredients(ingredients)
|
||||||
|
filters = session[:filters] || {}
|
||||||
|
if filters[:name].present?
|
||||||
|
ingredients = ingredients.where("name LIKE ?", "%#{filters[:name]}%")
|
||||||
|
end
|
||||||
|
ingredients
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -5,7 +5,11 @@
|
|||||||
|
|
||||||
<h3><%= t ".heading_diet" %></h3>
|
<h3><%= t ".heading_diet" %></h3>
|
||||||
<ul>
|
<ul>
|
||||||
<li><%= link_to t(".link_ingredients"), project_ingredients_path(@project) %></li>
|
<li>
|
||||||
|
<%= link_to t(".link_ingredients"), project_ingredients_path(@project) %>
|
||||||
|
/
|
||||||
|
<%= link_to t(".link_nutrients"), nutrients_project_ingredients_path(@project) %>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h3><%= t ".heading_common" %></h3>
|
<h3><%= t ".heading_common" %></h3>
|
||||||
|
19
app/views/ingredients/_filters.html.erb
Normal file
19
app/views/ingredients/_filters.html.erb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<fieldset id="filters" class="collapsible">
|
||||||
|
<legend onclick="toggleFieldset(this);"><%= l(:label_filter_plural) %></legend>
|
||||||
|
<%= form_tag url, id: 'filters_form', method: :get, remote: true do %>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td style="width:100%;"></td>
|
||||||
|
<td>
|
||||||
|
<%= text_field_tag 'filters[name]', session[:filters][:name],
|
||||||
|
placeholder: 'name filter' %>
|
||||||
|
</td>
|
||||||
|
<td style="padding-left:8px;">
|
||||||
|
<%= link_to l(:button_apply), '#',
|
||||||
|
:class => "icon icon-checked",
|
||||||
|
:onclick => '$("#filters_form").submit(); return false;' %>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<% end %>
|
||||||
|
</fieldset>
|
@ -6,7 +6,7 @@
|
|||||||
<th><%= l(:field_reference) %></th>
|
<th><%= l(:field_reference) %></th>
|
||||||
<th><%= l(:field_group) %></th>
|
<th><%= l(:field_group) %></th>
|
||||||
<th><%= l(:field_source) %></th>
|
<th><%= l(:field_source) %></th>
|
||||||
<th style="width:15%"><%= l(:field_action) %></th>
|
<th style="width:10%"><%= l(:field_action) %></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
id: 'add_nutrient_column', method: :post, remote: true do %>
|
id: 'add_nutrient_column', method: :post, remote: true do %>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td width="100%"></td>
|
<td style="width:100%"></td>
|
||||||
<td>
|
<td>
|
||||||
<%= select_tag 'id', nutrient_column_options %>
|
<%= select_tag 'id', nutrient_column_options %>
|
||||||
</td>
|
</td>
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
<%= render :partial => 'ingredients/form' %>
|
<%= render :partial => 'ingredients/form' %>
|
||||||
|
|
||||||
<h2><%= t ".heading" %></h2>
|
<h2><%= t ".heading" %></h2>
|
||||||
|
<%= render :partial => 'ingredients/filters',
|
||||||
|
:locals => {:url => filter_project_ingredients_path(@project)} %>
|
||||||
<div id='ingredients'>
|
<div id='ingredients'>
|
||||||
<%= render :partial => 'ingredients/list' %>
|
<%= render :partial => 'ingredients/list' %>
|
||||||
</div>
|
</div>
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
<%= render :partial => 'ingredients/form' %>
|
<%= render :partial => 'ingredients/form' %>
|
||||||
|
|
||||||
<h2><%= t ".heading" %></h2>
|
<h2><%= t ".heading" %></h2>
|
||||||
|
<%= render :partial => 'ingredients/filters',
|
||||||
|
:locals => {:url => filter_nutrients_project_ingredients_path(@project)} %>
|
||||||
<div id='nutrients'>
|
<div id='nutrients'>
|
||||||
<%= render :partial => 'ingredients/list_nutrients' %>
|
<%= render :partial => 'ingredients/list_nutrients' %>
|
||||||
</div>
|
</div>
|
||||||
|
@ -33,6 +33,7 @@ en:
|
|||||||
heading_common: 'Common'
|
heading_common: 'Common'
|
||||||
link_summary: 'Summary'
|
link_summary: 'Summary'
|
||||||
link_ingredients: 'Ingredients'
|
link_ingredients: 'Ingredients'
|
||||||
|
link_nutrients: 'Nutrients'
|
||||||
link_sources: 'Data sources'
|
link_sources: 'Data sources'
|
||||||
link_quantities: 'Quantities'
|
link_quantities: 'Quantities'
|
||||||
link_units: 'Units'
|
link_units: 'Units'
|
||||||
@ -61,8 +62,6 @@ en:
|
|||||||
groups:
|
groups:
|
||||||
other: 'other'
|
other: 'other'
|
||||||
meat: 'meat'
|
meat: 'meat'
|
||||||
options:
|
|
||||||
label_options: 'Options'
|
|
||||||
index:
|
index:
|
||||||
heading: 'Ingredients'
|
heading: 'Ingredients'
|
||||||
heading_nutrient_view: 'Nutrient view'
|
heading_nutrient_view: 'Nutrient view'
|
||||||
|
@ -7,10 +7,12 @@ resources :projects do
|
|||||||
post 'defaults', on: :collection
|
post 'defaults', on: :collection
|
||||||
end
|
end
|
||||||
resources :ingredients, :only => [:index, :create, :destroy] do
|
resources :ingredients, :only => [:index, :create, :destroy] do
|
||||||
post 'toggle', on: :member
|
|
||||||
post 'import', on: :collection
|
|
||||||
get 'nutrients', on: :collection
|
get 'nutrients', on: :collection
|
||||||
post 'toggle_nutrient_column', on: :collection
|
post 'toggle_nutrient_column', on: :collection
|
||||||
|
post 'toggle', on: :member
|
||||||
|
get 'filter', on: :collection
|
||||||
|
get 'filter_nutrients', on: :collection
|
||||||
|
post 'import', on: :collection
|
||||||
end
|
end
|
||||||
resources :sources, :only => [:index, :create, :destroy]
|
resources :sources, :only => [:index, :create, :destroy]
|
||||||
resources :quantities, :only => [:index, :create, :destroy] do
|
resources :quantities, :only => [:index, :create, :destroy] do
|
||||||
|
2
init.rb
2
init.rb
@ -16,7 +16,7 @@ Redmine::Plugin.register :body_tracking do
|
|||||||
project_module :body_tracking do
|
project_module :body_tracking do
|
||||||
permission :view_body_trackers, {
|
permission :view_body_trackers, {
|
||||||
:body_trackers => [:index],
|
:body_trackers => [:index],
|
||||||
:ingredients => [:index, :nutrients],
|
:ingredients => [:index, :nutrients, :filter, :filter_nutrients],
|
||||||
:sources => [:index],
|
:sources => [:index],
|
||||||
:quantities => [:index],
|
:quantities => [:index],
|
||||||
:units => [:index],
|
:units => [:index],
|
||||||
|
Reference in New Issue
Block a user