Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept unused exposures. Remove unexpose methods #667

Merged
merged 2 commits into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
- Added Action#url_without_query_params [#662](https://github.com/luckyframework/lucky/pull/662)

- Added `Lucky::AssetHelpers.load_manifest` so that API apps don't need a blank manifest to compile.

- Pages ignore unused exposures [#666](https://github.com/luckyframework/lucky/issues/666)

- `unexpose` and `unexpose_if_exposed` have been removed because they are no
longer necessary now that pages ignore unused exposures.
2 changes: 2 additions & 0 deletions UPGRADE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- `Query#preload` with a query now includes the association name -> [`Query#preload_{{ assoc_name }}`](https://github.com/luckyframework/lucky_record/pull/307)

- Remove `unexpose` and `unexpose_if_exposed` from your actions. Pages now ignore unused exposures.

### Upgrading from 0.11 to 0.12

- Upgrade Lucky CLI (macOS)
Expand Down
10 changes: 0 additions & 10 deletions spec/lucky/expose_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,9 @@ class MultipleExposeAndAssignsPage
end
end

class UnexposedAction < MultipleExposeAndAssigns
unexpose expose_one, expose_two
unexpose_if_exposed expose_three, something_that_was_not_exposed

get "/unexposed-action" do
render OnlyExposePage, name: "Bobby"
end
end

describe "exposures" do
it "works without explicit assigns" do
OnlyExpose.new(build_context, params).call
MultipleExposeAndAssigns.new(build_context, params).call
UnexposedAction.new(build_context, params).call
end
end
4 changes: 4 additions & 0 deletions spec/lucky/html_page_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ describe Lucky::HTMLPage do
InnerPage.new(build_context, foo: "bar").render.to_s.should contain %(<body>Inner textbar</body>)
end
end

it "accepts extra arguments so pages are more flexible with exposures" do
InnerPage.new(build_context, foo: "bar", ignore_me: true)
end
end

private def view
Expand Down
30 changes: 0 additions & 30 deletions src/lucky/exposeable.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module Lucky::Exposeable
macro included
EXPOSURES = [] of Symbol
UNEXPOSED = [] of Symbol

macro inherited
EXPOSURES = [] of Symbol
UNEXPOSED = [] of Symbol

inherit_exposures
end
Expand All @@ -15,9 +13,6 @@ module Lucky::Exposeable
\{% for v in @type.ancestors.first.constant :EXPOSURES %}
\{% EXPOSURES << v %}
\{% end %}
\{% for v in @type.ancestors.first.constant :UNEXPOSED %}
\{% UNEXPOSED << v %}
\{% end %}
end

# Sends the result of a method to the page as if it was passed as an argument.
Expand Down Expand Up @@ -113,29 +108,4 @@ module Lucky::Exposeable
macro expose(method_name)
{% EXPOSURES << method_name.id %}
end

# Removes the exposed method. Raises if method was not already exposed.
#
# Call this so that a previously exposed method will not be exposed.
macro unexpose(*method_names)
{% for method_name in method_names %}
{% if EXPOSURES.includes?(method_name.id) %}
{% UNEXPOSED << method_name.id %}
{% else %}
{% method_name.raise <<-ERROR
Can't unexpose '#{method_name}' because it was not previously exposed. Check the exposure name or use 'unexpose_if_exposed #{method_name}' if the exposure may or may not exist
ERROR
%}
{% end %}
{% end %}
end

# Removes the exposed method if it was exposed, otherwise does nothing.
#
# Call this so that a previously exposed method will not be exposed.
macro unexpose_if_exposed(*method_names)
{% for method_name in method_names %}
{% UNEXPOSED << method_name.id %}
{% end %}
end
end
1 change: 1 addition & 0 deletions src/lucky/html_builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Lucky::HTMLBuilder
{% has_default = declaration.value || declaration.value == nil %}
{% if var.stringify.ends_with?("?") %}{{ var }}{% end %} @{{ var.stringify.gsub(/\?/, "").id }} : {{ type }}{% if has_default %} = {{ declaration.value }}{% end %},
{% end %}
**unused_exposures
)
end
{% end %}
Expand Down
3 changes: 1 addition & 2 deletions src/lucky/renderable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ module Lucky::Renderable
{% for key, value in assigns %}
{{ key }}: {{ value }},
{% end %}
{% exposed = EXPOSURES.reject { |exposure| UNEXPOSED.includes?(exposure) } %}
{% for key in exposed %}
{% for key in EXPOSURES %}
{{ key }}: {{ key }},
{% end %}
)
Expand Down