Skip to content

Commit 9552efd

Browse files
author
Nils Henning
committed
update documentation, refactor first components
1 parent 80e062e commit 9552efd

File tree

10 files changed

+102
-41
lines changed

10 files changed

+102
-41
lines changed

.byebug_history

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
continue
2+
save_screenshot
3+
continue
4+
path
5+
::Rails.application.routes.url_helpers.send(property_path, params)
6+
property_path.is_a? Symbol
7+
property_path
8+
continue
9+
save_screenshot
10+
continue
11+
save_screenshot
12+
continue
13+
page.html
14+
continue
15+
save_screenshot
16+
page.html
17+
continue
18+
save_screenshot
19+
page.html
20+
continue
221
page.html
322
continue
423
save_screenshot
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%abbr{@tag_attributes}
2-
- if options[:text].blank? && block_given?
2+
- if text.blank? && block_given?
33
= yield
44
- else
5-
= options[:text]
5+
= text

app/concepts/matestack/ui/core/abbr/abbr.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Matestack::Ui::Core::Abbr
22
class Abbr < Matestack::Ui::Core::Component::Static
33
requires :title
4+
optional :text
45

56
def setup
67
@tag_attributes.merge!({
7-
"title": options[:title]
8+
"title": title
89
})
910
end
1011

app/concepts/matestack/ui/core/action/action.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
11
module Matestack::Ui::Core::Action
22
class Action < Matestack::Ui::Core::Component::Dynamic
3+
optional :path, :success, :failure, :notify, :confirm,
4+
method: { as: :action_method }, params: { as: :action_params }
35

46
def vuejs_component_name
57
'matestack-ui-core-action'
68
end
79

810
def setup
911
@component_config[:action_path] = action_path
10-
@component_config[:method] = options[:method]
11-
@component_config[:success] = options[:success]
12-
unless options[:success].nil?
13-
unless options[:success][:transition].nil?
14-
@component_config[:success][:transition][:path] = transition_path options[:success]
12+
@component_config[:method] = action_method
13+
@component_config[:success] = success
14+
unless success.nil?
15+
unless success[:transition].nil?
16+
@component_config[:success][:transition][:path] = transition_path success
1517
end
16-
unless options[:success][:redirect].nil?
17-
@component_config[:success][:redirect][:path] = redirect_path options[:success]
18+
unless success[:redirect].nil?
19+
@component_config[:success][:redirect][:path] = redirect_path success
1820
end
1921
end
20-
@component_config[:failure] = options[:failure]
21-
unless options[:failure].nil?
22-
unless options[:failure][:transition].nil?
23-
@component_config[:failure][:transition][:path] = transition_path options[:failure]
22+
@component_config[:failure] = failure
23+
unless failure.nil?
24+
unless failure[:transition].nil?
25+
@component_config[:failure][:transition][:path] = transition_path failure
2426
end
25-
unless options[:failure][:redirect].nil?
26-
@component_config[:failure][:redirect][:path] = redirect_path options[:failure]
27+
unless failure[:redirect].nil?
28+
@component_config[:failure][:redirect][:path] = redirect_path failure
2729
end
2830
end
29-
if options[:notify].nil?
31+
if notify.nil?
3032
@component_config[:notify] = true
3133
end
32-
if @component_config[:confirm] = options[:confirm]
33-
@component_config[:confirm_text] = options[:confirm].try(:[], :text) || "Are you sure?"
34+
if @component_config[:confirm] = confirm
35+
@component_config[:confirm_text] = confirm.try(:[], :text) || "Are you sure?"
3436
end
3537
end
3638

3739
def action_path
3840
begin
39-
if options[:path].is_a?(Symbol)
40-
return ::Rails.application.routes.url_helpers.send(options[:path], options[:params])
41+
if path.is_a?(Symbol)
42+
return ::Rails.application.routes.url_helpers.send(path, action_params)
4143
else
42-
return options[:path]
44+
return path
4345
end
4446
rescue
4547
raise "Action path not found"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%address{@tag_attributes}
2-
- if options[:text].nil? && block_given?
2+
- if text.nil? && block_given?
33
= yield
44
- else
5-
= options[:text]
5+
= text
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Matestack::Ui::Core::Address
22
class Address < Matestack::Ui::Core::Component::Static
3-
3+
optional :text
44
end
55
end

app/concepts/matestack/ui/core/area/area.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
module Matestack::Ui::Core::Area
22
class Area < Matestack::Ui::Core::Component::Static
3+
optional :alt, :coords, :download, :href, :hreflang, :media, :rel, :shape, :target, :type
4+
35
def setup
46
@tag_attributes.merge!({
5-
alt: options[:alt],
6-
coords: options[:coords].join(','),
7-
download: options[:download],
8-
href: options[:href],
9-
hreflang: options[:hreflang],
10-
media: options[:media],
11-
rel: options[:rel],
12-
shape: options[:shape],
13-
target: options[:target],
14-
type: options[:type]
7+
alt: alt,
8+
coords: coords.join(','),
9+
download: download,
10+
href: href,
11+
hreflang: hreflang,
12+
media: media,
13+
rel: rel,
14+
shape: shape,
15+
target: target,
16+
type: type
1517
})
1618
end
1719
end

app/lib/matestack/ui/core/properties.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ def self.included(base)
1717
# initializer calls super and creates instance methods for defined required and optional properties afterwards
1818
module Initializer
1919
def initialize(model=nil, options={})
20+
options = model.dup if options.empty? && model.is_a?(Hash)
21+
required_hooks(options)
22+
optional_hooks(options)
2023
super
21-
required_hooks
22-
optional_hooks
2324
end
2425
end
2526

@@ -55,7 +56,7 @@ def requires_properties
5556
end
5657
end
5758

58-
def optional_hooks
59+
def optional_hooks(options)
5960
self.class.optional_properties.compact.each do |prop|
6061
if prop.is_a? Array
6162
hash = prop.flatten
@@ -69,14 +70,14 @@ def optional_hooks
6970
end
7071
end
7172

72-
def required_hooks
73+
def required_hooks(options)
7374
self.class.requires_properties.compact.each do |prop|
7475
if prop.is_a? Array
7576
hash = prop.flatten
7677
options[hash.last[:as]] = options[hash.first]
7778
prop = hash.last[:as]
7879
end
79-
raise PropertyMissingException, "Required property #{prop} is missing for #{self.class}" if self.send(:options)[prop].nil?
80+
raise PropertyMissingException, "Required property #{prop} is missing for #{self.class}" if options[prop].nil?
8081
raise PropertyOverwritingExistingMethodException, "Required property #{prop} would overwrite already defined instance method for #{self.class}" if self.respond_to? prop
8182
send(:define_singleton_method, prop) do
8283
options[prop]

docs/concepts/component.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ class SomeComponent < Matestack::Ui::StaticComponent
167167
end
168168
```
169169

170-
#### Options validation
170+
Some common names that could not be used as properties:
171+
```ruby
172+
:method,
173+
:params
174+
```
175+
176+
#### Property validations
171177

172178
Coming soon, stay tuned!
173179

spec/0.8/components/base/properties_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,36 @@ def response
160160
expect(another_component.test).to eq('Response')
161161
end
162162

163+
it 'should be accesible in setup' do
164+
class SetupComponent < Matestack::Ui::StaticComponent
165+
requires :title, :desc
166+
def setup
167+
@foo = title
168+
end
169+
def prepare
170+
@bar = desc
171+
end
172+
def response
173+
paragraph text: @foo
174+
paragraph text: @bar
175+
end
176+
register_self_as :setup_component
177+
end
178+
class ExamplePage < Matestack::Ui::Page
179+
def response
180+
setup_component title: 'Foo', desc: 'Bar'
181+
end
182+
end
183+
184+
visit '/example'
185+
static_output = page.html
186+
expected_static_output = <<~HTML
187+
<p>Foo</p>
188+
<p>Bar</p>
189+
HTML
190+
expect(stripped(static_output)).to include(stripped(expected_static_output))
191+
end
192+
163193
it 'should work with slots' do
164194
class SlotComponent < Matestack::Ui::StaticComponent
165195
requires slot: { as: :some_slot }

0 commit comments

Comments
 (0)