Skip to content

Commit

Permalink
Fixed support for hash properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Jul 3, 2012
1 parent fd0a6ed commit 82b4526
Show file tree
Hide file tree
Showing 36 changed files with 358 additions and 127 deletions.
14 changes: 11 additions & 3 deletions app/models/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,23 @@ def type_cast(value)
end
elsif ptype == 'hash'
if value.kind_of?(Hash)
StringHash[value]
else
nil
StringHash.from_hash(value)
elsif value.kind_of?(String)
StringHash.from_string(value)
end
else
nil
end
end

def merge_hash(orig, value)
unless orig.kind_of?(StringHash)
orig = StringHash.from_hash(orig)
end
orig.merge!(value)
return orig
end

def klass
if ptype == 'hash'
StringHash
Expand Down
60 changes: 60 additions & 0 deletions app/models/string_hash.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
class StringHash < Hash
include RubyLess

def self.from_string(str)
from_hash(JSON.parse(str))
end

def self.from_hash(hash)
obj = new
hash.each do |k,v|
obj[k.to_s] = v.to_s
end
obj
end

def self.[](value)
from_hash(value)
end

# Deserialization used by Property
def self.json_create(serialized)
if data = serialized['data']
StringHash[data]
else
nil
end
end

def []=(k, v)
if v.blank?
delete(k.to_s)
else
super(k.to_s, v.to_s)
end
end

def merge!(value)
value.each do |k,v|
self[k] = v
end
self
end

def merge(value)
obj = dup
value.each do |k,v|
obj[k] = v
end
obj
end

# Serialization used by Property
def to_json(*args)
{ 'json_class' => 'StringHash', 'data' => Hash[self] }.to_json
end

# This is used in case we show a form with the StringHash so that the value
# is not serialized to junk. This is the other side of "from_string".
def to_s
Hash[self].to_json
end

safe_context [:[], String] => String
safe_method :keys => [String]
end
2 changes: 1 addition & 1 deletion app/views/templates/document_create_tabs/_import.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<label for='node_klass'><%= _('class of first element') %></label>
<%= select('node', 'klass', Node.classes_for_form, :selected => 'Page' ) %><br/>

<% if @node.can_publish? && !visitor.site.auto_publish? %>
<% if @node.can_publish? && !@node.auto_publish? %>
<label for='node_v_status'><%= _('publish nodes') %></label>
<small><input type='checkbox' name='node[v_status]' value='50'/> <%= _('pub') %></small><br/>
<% end -%>
Expand Down
2 changes: 1 addition & 1 deletion app/views/versions/custom_tab.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<label for='<%= col.name %>'><%= _(col.name) %></label>
<% case col.ptype
when :datetime %>
<%= date_box(@node, col.name, :size=>15, :value => @node.prop[col.name]) %>
<%= date_box(@node, "node[#{col.name}]", :size=>15, :value => @node.prop[col.name]) %>
<% else %>
<%= text_area 'node', col.name, :cols => nil, :rows => 1, :value => @node.prop[col.name], :class => 'full_width' %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/gems.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ simple_xlsx_writer: # spreadsheet
querybuilder: '= 1.1.1'
yamltest: '= 0.7.0'
rubyless: '= 0.8.6'
property: '= 2.3.0'
property: '= 2.3.2'
versions: '= 0.3.1'

jeweler:
Expand Down
20 changes: 9 additions & 11 deletions lib/zafu/parsing_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,16 @@ def scan
#puts "SCAN(#{@method}): [#{@text[0..20]}]"
if @text =~ %r{\A([^<]*?)(\s*)//!}m
# comment
flush $1
found = $1
flush found
eat $2
scan_comment
elsif @text =~ /\A([^<]*)</m
found = $1
if found =~ /^ *$/
eat found
space = found
else
flush found
space = ''
end
elsif @text =~ /\A(([^<]*?)(^ *|))</m
# Warning: this regexp looks too complicated but it's the only one that
# works without capturing too much or not enough space in "space_before".
flush $2
space = $3
eat space

if @text[1..1] == '/'
store space
Expand All @@ -149,7 +147,7 @@ def scan
elsif @text[0..8] == '<![CDATA['
store space
flush '<![CDATA['
elsif found.last == ' ' && @text[0..1] == '< '
elsif $1.last == ' ' && @text[0..1] == '< '
# solitary ' < '
store space
flush '< '
Expand Down
8 changes: 1 addition & 7 deletions lib/zafu/process/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ def r_each

if join = @params[:join]
join = RubyLess.translate_string(self, join)
#if join_clause = @params[:join_if]
# set_stored(Node, 'prev', "#{var}_prev")
# cond = get_test_condition(var, :test=>join_clause)
# out "<%= #{var}_prev = #{node}[#{var}_index - 1]; (#{var}_index > 0 && #{cond}) ? #{join.inspect} : '' %>"
#else
out "<%= #{var}_index > 0 ? #{join} : '' %>"
#end
out "<%= #{var}_index > 0 ? #{join} : '' %>"
end

if alt_class = @params[:alt_class]
Expand Down
2 changes: 1 addition & 1 deletion lib/zena.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def use(*args)
def init
config = Rails.configuration
enable_tools
puts "** zena #{Zena::VERSION} starting (c) Gaspard Bucher 2011"
puts "** zena #{Zena::VERSION} starting (c) Gaspard Bucher 2012"
puts "** Bricks: #{Bricks::CONFIG.map{|k,v| k}.sort.join(', ')}"

add_load_paths(config)
Expand Down
2 changes: 1 addition & 1 deletion lib/zena/acts/enrollable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def prepare_roles(force = false)
next unless role.class == Role && role.id
role_ids << role.id if role.column_names & keys != []
end

prop['cached_role_ids'] = role_ids
end

Expand Down
4 changes: 2 additions & 2 deletions lib/zena/use/ajax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def update_page_content(page, obj)
end
end
if params[:redir]
page << "window.location.href = '#{params[:redir]}'"
page << "window.location.href = '#{params[:redir]}';"
end
if params[:reload]
page << "Zena.reload(#{params[:reload].inspect})"
page << "Zena.reload(#{params[:reload].inspect});"
end
page << render_js(false)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/zena/use/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def r_set
var = var.to_s
begin
typed_string = ::RubyLess.translate(self, code)

# Do we have this variable already ?
if up_var = get_context_var('set_var', var)
if up_var.klass != typed_string.klass
Expand All @@ -210,7 +211,7 @@ def r_set
set_context_var('set_var', var, RubyLess::TypedString.new(name, typed_string.opts))
end
rescue RubyLess::NoMethodError => err
parser_error(err.message, code)
out parser_error(err.message, "set #{var}=#{code}")
end
end
expand_with
Expand Down
4 changes: 2 additions & 2 deletions lib/zena/use/dates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def date_box(obj, name, opts = {})
# fld = Zafu::Markup.new('input')
# fld.params = (opts[:html] || {}).merge(:name => "node[#{name}]", :id => opts[:id], :type => 'text', :value => value)
if opts[:size]
fld = "<input id='#{opts[:id]}' name='node[#{name}]' type='text' size='#{opts[:size]}' value='#{value}' class='#{opts[:class]}'/>"
fld = "<input id='#{opts[:id]}' name='#{name}' type='text' size='#{opts[:size]}' value='#{value}' class='#{opts[:class]}'/>"
else
fld = "<input id='#{opts[:id]}' name='node[#{name}]' type='text' value='#{value}' class='#{opts[:class]}'/>"
fld = "<input id='#{opts[:id]}' name='#{name}' type='text' value='#{value}' class='#{opts[:class]}'/>"
end

js_data << %Q{Calendar.setup({
Expand Down

0 comments on commit 82b4526

Please sign in to comment.