leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 215 forks source link

Custom filter for owned items #201

Closed l3iodeez closed 9 years ago

l3iodeez commented 9 years ago

I have a wice_grid that displays information of the materials used in buildings belonging to customers in the view 'welcome'.

I have set up my controller so that the admin will see all buildings listed, but normal users only see buildings that belong to the Customer to which they belong. So far so good. The only problem is that normal users still see the names of all buildings in the custom filter dropdown for buildingname. I would like the dropdown to only display items that are owned by the customer to which the current user is attached.

Is there a way to do this in wice_grid? None of the examples seem to work this way.

welcome_controller .rb:

class WelcomeController < ApplicationController

def index
    unless current_user.is_admin
     @customer = current_user.customer
     cust_mats = current_user.customer.materials

    @materials_grid = initialize_grid(cust_mats,
      :include => [:space, :customer, :building],
      :conditions => {:customer => @customer},
      :per_page => 10,
      :enable_export_to_csv => true,
      :csv_file_name => 'Survey Data'
      )

else

    @materials_grid = initialize_grid(Material,
      :include => [:space, :customer, :building],
      :per_page => 10,
      :enable_export_to_csv => true,
      :csv_file_name => 'Survey Data'
      )
    end
   end
end

/views/welcome/_grid.html.erb - I have other fields but this is the only one for which I need to do this.

<%= grid(@materials_grid, show_filters: :always) do |g|
g.column name: 'Building', attribute: 'buildingname', model: 'Building', custom_filter: :auto, auto_reload: true  do |material|
    material.building.buildingname if material.building

  end 
leikind commented 9 years ago

for normal users custom_filter should receive a hash with the options normal people can see, not :auto

l3iodeez commented 9 years ago

Thanks. For anyone else with this issue simply iterate through the items in your controller and add them to a hash. Then set custom_filter: to that hash in your grid partial.

  @ownedbuildings = Hash.new()

  current_user.customer.buildings.each do |building|

  @ownedbuildings[building.buildingname] = building.buildingname

  end
leikind commented 9 years ago

this is ruby, not php. Use map.