Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix updating of items

  • Loading branch information...
commit 504c446f0517101f645e0eda6c9b7e73f6a219f6 1 parent 5fd0aa4
@GordonDiggs authored
View
6 cataloguais.rb
@@ -18,8 +18,6 @@
require_relative "models/item"
DataMapper.finalize
- settings.fields << 'Created At'
-
# set the input width based on the number of fields
set :item_width, 840 / settings.fields.count
@@ -72,7 +70,9 @@
end
get '/' do
+ @fields = settings.fields + ['Added On']
@sort = if params[:sort]
+ params[:sort].gsub!(/added_on/, 'created_at')
[params[:sort]] + (settings.sort_order - [params[:sort]])
else
settings.sort_order
@@ -114,6 +114,7 @@
end
post '/update/:id' do
+ params[:item].delete('added_on')
item = Item.first(:id => params[:id])
item.attributes = params[:item]
item.save!
@@ -170,6 +171,7 @@
# render the row of the table for a given partial
def item_table_row(item)
+ @fields ||= settings.fields + ['Added On']
@item = item
haml :_item, :layout => false
end
View
4 models/item.rb
@@ -37,8 +37,8 @@ def self.search_and_sort(sort, direction = :asc, search = '')
Item.find_by_sql("SELECT * FROM items ORDER BY #{sort_options} #{direction}").select { |item| item.to_s.downcase.include? search.to_s.downcase }
end
- def created_at
- self.attribute_get(:created_at).strftime("%m.%d.%Y") if self.attribute_get(:created_at)
+ def added_on
+ self.created_at.strftime("%m.%d.%Y") if self.created_at
end
def to_a
View
4 views/_item.haml
@@ -1,5 +1,5 @@
%tr{ 'data-id' => @item.id }
- - settings.fields.each do |field|
+ - @fields.each do |field|
%td{ :style => "max-width:#{settings.item_width + 100}px" }= @item.send(field.robotize)
- if session[:editing_enabled]
%td.edit
@@ -9,7 +9,7 @@
%tr.edit{ 'data-id' => @item.id }
%form.ajax{ :action => "/update/#{@item.id}", :method => 'post' }
- - settings.fields.each do |field|
+ - @fields.each do |field|
%td
%input{ :type => 'text', :name => "item[#{field.robotize}]", :placeholder => field, :value => @item.send(field.robotize) }
%td.edit
View
9 views/index.haml
@@ -3,17 +3,16 @@
#add-new
%form.ajax{ :action => '/new', :method => 'post' }
- settings.fields.each do |field|
- - next if field.robotize == 'created_at'
%input{ :type => 'text', :name => "item[#{field.robotize}]", :placeholder => field, :style => "width:#{(0.8/settings.fields.count)*100}%" }
%input{ :type => 'submit', :value => 'Add' }
%table{ :cellspacing => 0 }
%tr
- - settings.fields.each do |field|
+ - @fields.each do |field|
%th
- - next_direction = (@sort[0] == field.robotize) ? opposite_direction : :asc
- %a{ :href => "/?sort=#{field.robotize}&direction=#{next_direction}&search=#{params[:search]}" }= field.gsub(/Created At/, 'Added On')
- - if @sort[0] == field.robotize
+ - next_direction = (@sort[0].gsub(/created_at/, 'added_on') == field.robotize) ? opposite_direction : :asc
+ %a{ :href => "/?sort=#{field.robotize}&direction=#{next_direction}&search=#{params[:search]}" }= field
+ - if @sort[0].gsub(/created_at/, 'added_on') == field.robotize
%span= (@direction == :asc) ? "\u2191" : "\u2193"
- @items.each do |item|
= item_table_row(item)
Please sign in to comment.
Something went wrong with that request. Please try again.