Hi Scott,

Interesting, I never would have thought a model was the place for
this.
It's certainly neater, I'll have a go at implementing it like this and
let you know how it turns out.

Thanks :)

On Jul 21, 9:52 am, Harvey <[email protected]> wrote:
> I would attack this sort of problem a bit different by making the
> AwesomeGrid module a standard rails model.
>
> The advantage of doing it this way is you can easily keep your view
> rendering logic out of you models and also keep your model file in
> your models directory instead of in your lib directory.
>
> Here is a quick run down of what I would do, let me know if you have
> questions.
>
> Scott
>
> -- app/models/awesome_grid.rb
> class AwesomeGrid
>   attr_reader :first, :last, :count
>
>   def initialize(options)
>     @model = options.delete(:model)
>     @params = options.delete(:params)
>     @columns = options.delete(:columns)
>     @options = options
>   end
> end
>
> -- app/controllers/products_controller.rb
> class ProductsController < ApplicationController
>   def index
>     @awesome_grid = AwesomeGrid.new(
>       :model => Product,
>       :params => params,
>       :columns => ['title', 'description'],
>       :options => {:actions => ['show', 'edit', 'delete']}
>     )
>   end
> end
>
> -- app/views/products/index.html.erb
> render :partial => "controls/awesome_grid", :object => @grid
>
> -- app/views/controls/_awesome_grid.html.erb
> <div class="grid">
>   <p class="search-box">
>     <form>
>       <input type="text" value="<%= awesome_grid.search %>"
> name="search" id="search">
>       <input type="submit" value="Search" />
>     </form>
>   </p>
>   <p class="pagination">Displaying <%= awesome_grid.first %>–<%=
> awesome_grid.last %> of <%= awesome_grid.count %></p>
> ...
>
> On Jul 20, 9:30 pm, markbrown4 <[email protected]> wrote:
>
> > I managed to get it working by:
>
> > changing the name of the partial _awesome_grid.erb
>
> > -- app/views/products/index.html.erb
> > render :partial => "controls/awesome_grid",
> >   :locals => Controls::AwesomeGrid::get_data(
> >   Product,
> >   params,
> >   ['title', 'description'],
> >   {:actions => ['show', 'edit', 'delete']}
> > )
>
> > -- lib/controls/awesome_grid.rb
> > module Controls
> >   module AwesomeGrid
> >     def self.get_data(model, params, columns = [], options = {})
> >       return {
> >         ...
> >       }
> >     end
> >   end
> > end
>
> > Does this seem like an appropriate use of these files or would you
> > arrange them differently?
>
> > Thanks,
>
> > On Jul 20, 4:21 pm, markbrown4 <[email protected]> wrote:
>
> > > Hi,
>
> > > I'm trying to implement a grid control with code in the following
> > > places. no worky.
>
> > > app/views/controls/_AwesomeGrid.erb
> > > app/views/products/index.html.erb
> > > lib/controls/awesome_grid.rb
>
> > > -- A partial for the HTML output
> > > <div class="grid">
> > >     <p class="search-box"><form><input type="text" value="<%= search
> > > %>" name="search" id="search"> <input type="submit" value="Search" /></
> > > form></p>
> > >     <p class="pagination">Displaying <%= first %>–<%= last %> of <%=
> > > count %></p>
> > > ...
>
> > > -- Embedded in a view
> > > <h1>Listing products</h1>
> > > <%= AwesomeGrid::grid(Product,
> > >     ['title', 'description'], {
> > >         :actions => ['show', 'edit', 'delete']
> > >     }
> > > ) %>
>
> > > -- a module to take the options prepare the data and send to the
> > > partial.
> > > module AwesomeGrid
> > >   def self.grid(model, columns = [], options = {})
> > >     options = {
> > >       :page_size => 10,
> > >       :actions => ['show'],
> > >       :order_by => columns[0],
> > >       :search_by => columns[0]
> > >     }.merge(options)
>
> > >     ...
>
> > >     render :partial => "controls/AwesomeGrid", :locals => {
> > >        :model => model,
> > >        :columns => columns,
> > >        :options => options,
> > >        :search => @search,
> > >        :page => @page,
> > >        :first => @first,
> > >        :last => @last,
> > >        :count => @count,
> > >        :number_of_pages => @number_of_pages
> > >    }
>
> > >    end
> > > end
>
> > > --
>
> > > Is this anywhere close to how something like this should look?
> > > I realise that the controller is normally the place for the heavy
> > > lifting but I am wanting to keep all of the querying, searching,
> > > pagination and UI in one place and not have to handle this in all of
> > > the controllers of the app.
>
> > > Thanks in advance :)
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to