Skip to content

Commit

Permalink
Sorting the type declarations from needs so the nillable ones are las… (
Browse files Browse the repository at this point in the history
#993)

* Sorting the type declarations from needs so the nillable ones are last in the constructor. Fixes #992

* Account for when a needs has a default value that isn't nilable.
Moved the test spec to assignable where other needs are tested

* make sure that type declarations with a default of false and nil are also moved to the back of the list

* added a few more needs just to be sure we're covering edge cases. I hope that's all of them
  • Loading branch information
jwoertink authored Dec 23, 2019
1 parent 9093e56 commit e421076
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions spec/lucky/assignable_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "../spec_helper"

include ContextHelper

class BasePage
include Lucky::HTMLPage

Expand Down Expand Up @@ -41,11 +43,26 @@ class PageWithQuestionMark
end
end

class PageWithDefaultsFirst
include Lucky::HTMLPage
needs required : String
needs nothing : Bool = false
needs extra_css : String?
needs extra_html : String? = nil
needs status : String = "special"
needs title : String

def render
text "#{@status} #{@title}"
end
end

describe "Assigns within multiple pages with the same name" do
it "should only appear once in the initializer" do
PageOne.new build_context, title: "foo", name: "Paul", second: "second"
PageTwo.new build_context, title: "foo", name: "Paul"
PageThree.new build_context, name: "Paul", admin_name: "Pablo", title: "Admin"
PageWithQuestionMark.new(build_context, signed_in?: true).perform_render.to_s.should contain("true")
PageWithDefaultsFirst.new(build_context, required: "thing", title: "foo").perform_render.to_s.should contain("special foo")
end
end
5 changes: 4 additions & 1 deletion src/lucky/html_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ module Lucky::HTMLBuilder

macro generate_needy_initializer
{% if !@type.abstract? %}
{% sorted_assigns = ASSIGNS.sort_by { |dec|
dec.type.resolve.nilable? || dec.value || dec.value == nil || dec.value == false ? 1 : 0
} %}
def initialize(
{% for declaration in ASSIGNS %}
{% for declaration in sorted_assigns %}
{% var = declaration.var %}
{% type = declaration.type %}
{% value = declaration.value %}
Expand Down

0 comments on commit e421076

Please sign in to comment.