Skip to content

Commit

Permalink
Added 'show' option to [checkbox].
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Nov 30, 2010
1 parent 166abf9 commit 84bf7aa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
17 changes: 17 additions & 0 deletions db/migrate/20101123125822_add_integer_idx.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddIntegerIdx < ActiveRecord::Migration
def self.up
# index float for nodes
create_table :idx_nodes_integers, :options => Zena::Db.table_options do |t|
t.integer 'node_id', :null => false
t.string 'key'
t.integer 'value'
end
add_index(:idx_nodes_integers, [:node_id, :key])
add_index(:idx_nodes_integers, :value)
add_index(:idx_nodes_integers, :node_id)
end

def self.down
drop_table 'idx_nodes_integers'
end
end
13 changes: 13 additions & 0 deletions db/migrate/20101130134522_add_index_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddIndexField < ActiveRecord::Migration
def self.up
add_column :nodes, :idx_integer1, :integer
add_index :nodes, :idx_integer1
add_column :nodes, :idx_integer2, :integer
add_index :nodes, :idx_integer2
end

def self.down
remove_column :nodes, :idx_integer1
remove_column :nodes, :idx_integer2
end
end
31 changes: 18 additions & 13 deletions lib/zena/use/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,25 @@ def make_checkbox(node, opts)
else
# literal values
list, name, selected = opts[:list], opts[:name], opts[:selected]
if opts[:selected].kind_of?(Array)
show = opts[:show] || list
if selected.kind_of?(Array)
selected = selected.map(&:to_s)
name = "node[#{name}][]"
else
selected = [selected]
selected = [selected.to_s]
name = "node[#{name}]"
end
res = ["<div class='input_checkbox'>"]
list.each do |value|
list.each_with_index do |value, i|

res << "<span><input type='checkbox' name='#{name}' value='#{value}'"
res << (selected.include?(value.to_s) ? " checked='checked'/> " : '/> ')
res << value.to_s
res << show[i]
res << '</span> '
end
res << '</div>'
res << "<input type='hidden' name='#{name}' value=''/>"
res.join('')
end
end
end # ViewMethods
Expand Down Expand Up @@ -457,11 +460,14 @@ def r_checkbox
if values
return parser_error("missing attribute 'name'") unless name = @params[:name]
# parse literal values
finder = values.split(',').map(&:strip).inspect
opts = [":name => #{name.inspect}", ":list => #{values.split(',').map(&:strip).inspect}"]
if show_values = @params[:show]
opts << ":show => #{show_values.split(',').map(&:strip).inspect}"
end
meth = RubyLess.translate(node.klass, name)
meth = "#{node}.#{meth}"
out "<%= make_checkbox(#{node}, :list => #{finder}, :name => #{name.inspect}, :selected => #{meth}) %>"
opts << ":selected => #{node}.#{meth}"

out "<%= make_checkbox(#{node}, #{opts.join(', ')}) %>"
else
if name = @params[:name]
if name =~ /(.*)_ids?\Z/
Expand All @@ -484,11 +490,10 @@ def r_checkbox
return parser_error("invalid class (#{finder[:class]})") unless finder[:class].first <= Node
finder = finder[:method]
end

attribute = @params[:attr] || 'title'
out "<%= make_checkbox(#{node}, :list => #{finder}, :role => #{meth.inspect}, :attr => #{attribute.inspect}) %>"
end

attribute = @params[:attr] || 'title'

out "<%= make_checkbox(#{node}, :list => #{finder}, :role => #{meth.inspect}, :attr => #{attribute.inspect}) %>"
end

alias r_radio r_checkbox
Expand Down
6 changes: 6 additions & 0 deletions test/integration/zafu_compiler/forms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ checkbox_literal:
src: "<r:checkbox name='assigned' values='gaspard,sophie'/>"
res: "/value='gaspard' checked='checked'/> gaspard/"

checkbox_literal_show:
context:
node: 'people'
src: "<r:checkbox name='assigned' values='gaspard,sophie' show='slave,master'/>"
res: "/value='gaspard' checked='checked'/> slave/"

checkbox_finder:
context:
node: 'opening'
Expand Down
2 changes: 1 addition & 1 deletion zena.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["Gaspard Bucher"]
s.date = %q{2010-11-29}
s.date = %q{2010-11-30}
s.default_executable = %q{zena}
s.description = %q{zena is a Ruby on Rails CMS (content managment system) with a focus on usability, ease of customization and web 2.0 goodness (application like behaviour).}
s.email = %q{gaspard@teti.ch}
Expand Down

0 comments on commit 84bf7aa

Please sign in to comment.