Skip to content

Commit

Permalink
Replaced [date] with [main_date] to avoid clash when a property is ca…
Browse files Browse the repository at this point in the history
…lled 'date'.
  • Loading branch information
gaspard committed Jan 24, 2011
1 parent a7a646e commit 0c8f5ac
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
16 changes: 7 additions & 9 deletions lib/zena/use/dates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Common

# This is like strftime but with better support for i18n (translate day names, month abbreviations, etc)
def format_date(thedate, opts = {})
return '' unless thedate
return '' if thedate.blank?

theformat, tz_name, lang = opts[:format], opts[:tz], opts[:lang]
format = theformat || '%Y-%m-%d %H:%M:%S'
Expand All @@ -28,10 +28,8 @@ def format_date(thedate, opts = {})
begin
adate = Date.parse(thedate)
utc_date = adate

rescue
# only return error if there is a format (without = used in sql query)
return theformat ? "<span class='parser_error'>invalid date #{thedate.inspect}</span>" : Time.now.strftime('%Y-%m-%d %H:%M:%S')
return "<span class='parser_error'>invalid date #{thedate.inspect}</span>"
end
else
adate = thedate
Expand Down Expand Up @@ -250,11 +248,11 @@ module ZafuMethods
include RubyLess
safe_method_for Time, :year => {:class => Number, :pre_processor => true}
safe_method_for Time, [:strftime, String] => {:class => String, :pre_processor => true}
safe_method :date => :get_date
safe_method :main_date => :get_date
safe_method [:parse_date, String] => {:class => Time, :nil => true, :accept_nil => true}

def get_date(signature)
if var = get_context_var('set_var', 'date')
if var = get_context_var('set_var', 'main_date')
{:class => var.klass, :method => var, :nil => var.could_be_nil?}
else
{:class => Time, :method => 'main_date'}
Expand All @@ -279,7 +277,7 @@ def r_uses_datebox
end

# Select a date for the current context
def r_date
def r_main_date
return nil unless code = get_attribute_or_eval

if format = @params[:format]
Expand All @@ -300,9 +298,9 @@ def r_date
else
return parser_error("should evaluate to a String or Time (found #{code.klass})")
end
v = get_var_name('set_var', 'date')
v = get_var_name('set_var', 'main_date')
out "<% #{v} = #{code} -%>"
set_context_var('set_var', 'date', RubyLess::TypedString.new(v, :class => Time, :nil => could_be_nil))
set_context_var('set_var', 'main_date', RubyLess::TypedString.new(v, :class => Time, :nil => could_be_nil))
out expand_with
end

Expand Down
16 changes: 12 additions & 4 deletions test/integration/zafu_compiler/dates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ show_date_foo:
context:
date: '2010-05-16'
lang: fr
src: "<r:show eval='date' format='%A %d.%m.%Y'/>"
src: "<r:show eval='main_date' format='%A %d.%m.%Y'/>"
tem: "/main_date/"
res: 'dimanche 16.05.2010'

Expand Down Expand Up @@ -114,13 +114,21 @@ date_from_params:
date_from_params_in_query:
context:
year: 2006
src: "<r:date text='#{params[:year]}/04/03' format='%Y/%m/%d' do='show' eval='date' format='%Y-%m-%d'/>"
tem: "/_zdate = \"#\{params\[:year\]\}/04/03\".to_utc\(\"%Y/%m/%d\"\) -%>/"
src: "<r:main_date text='#{params[:year]}/04/03' format='%Y/%m/%d' do='show' eval='main_date' format='%Y-%m-%d'/>"
tem: "/_zmaindate = \"#\{params\[:year\]\}/04/03\".to_utc\(\"%Y/%m/%d\"\) -%>/"
res: "2006-04-03"

input_date:
context:
node: opening
src: "<r:Post?><r:input type='date' name='date'/></r:Post?>"
tem: '/date_box\(@node, "date", :size=>15, :value => @node.prop\[.date.\]\)/'
res: "/value='2011-01-16 00:00'/"
res: "/value='2011-01-16 01:00'/"

test_date:
src: "<r:Post?><r:if test='date'></r:if></r:Post?>"
tem: "/@node.prop\['date'\]/"

format_date_empty_string:
tem: "<%= format_date('') %>"
res: ""
2 changes: 1 addition & 1 deletion test/integration/zafu_compiler/roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ list_relations_filter_group:
res: "reference, reference_for, set_tag"

input_relation_name:
src: "<r:select param='rel' eval='Contact.relations(\"doc\").map(:name)'/>"
src: "<r:select name='rel' eval='Contact.relations(\"doc\").map(:name)'/>"
res: "<select name='node[rel]'><option value=\"reference\">reference</option>\n<option value=\"reference_for\">reference_for</option>\n<option value=\"set_tag\">set_tag</option></select>"

manual_select:
Expand Down
2 changes: 1 addition & 1 deletion test/integration/zafu_compiler/urls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ link_date:
context:
ref_date: '2008-06-27'
old_src: "<r:link date='current_date'/>"
src: "<r:link date='#{date}'/>"
src: "<r:link date='#{main_date}'/>"
tem: '/:date => "#\{main_date\}"'
res: "<a href='/oo/projects-list/Clean-Water-project/page22.html?date=2008-06-27T00%3A00%3A00%2B00%3A00'>status title</a>"

Expand Down
2 changes: 2 additions & 0 deletions test/unit/zena/use/dates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ def test_date_box
visiting(:status)
assert_match %r{span class="date_box".*img src="\/calendar\/iconCalendar.gif".*input id='datef.*' name='node\[updated_at\]' type='text' value='2006-04-11 00:00'}m, date_box(@node, 'updated_at')
end

# see dates.yml
end
4 changes: 3 additions & 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{2011-01-19}
s.date = %q{2011-01-24}
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 Expand Up @@ -597,6 +597,8 @@ Gem::Specification.new do |s|
"locale/fr/zena.mo",
"locale/fr/zena.po",
"locale/zena.pot",
"misc/zena",
"misc/zena_init",
"old_zafu/action.rb",
"old_zafu/ajax.rb",
"old_zafu/attributes.rb",
Expand Down

0 comments on commit 0c8f5ac

Please sign in to comment.