passionview 0.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 34920798fb9297df7af39a1db7464eedbb655fae
4
- data.tar.gz: b049349b6e5d29e347629c06e0d20ddc7a2945e4
2
+ SHA256:
3
+ metadata.gz: 5718490892e384b45b6dade16f47453507efef16a8f25145cc250050365c0028
4
+ data.tar.gz: 5d12ab028481f3a94ab39bbef2823cdc99e5e9682e671288c16f3af0cee7d9a0
5
5
  SHA512:
6
- metadata.gz: a20a6494bf9610b53e2abc197d7c1ad68a51123179580a9b155aa62e476976561b3dba52fbb23867f9d73473903d9925898beda3484e00a4a8652281a8b0ba10
7
- data.tar.gz: 64958934729f7c60c7afb5272a3e8762be618802526e78ff377d9bf7e577441432249b07a5108cb2d272f318c2bde80fbb109067765a3173f48c0a3a446df829
6
+ metadata.gz: 4c064b4008f3426b7ab8c8c8c7c9f141a61fe1f9ee1eff6c0803ef4e1520a8a90bb66deeeb5a807fd36190afb80ee7d2f084c803e79143d737a7e4865b58dd7f
7
+ data.tar.gz: fb96e51720c8bfa9b174e29b19bddc2e2c52449112f65cb61e4a2a40eb5e4383a07828aa4cbac52cad58b3c1b890e164dfc7b776a8c2e30c9a2f81a1b42d2c5a
@@ -55,7 +55,28 @@ module PassionView::Actionable
55
55
  end
56
56
  end
57
57
 
58
+ def actions
59
+ return @actions unless @actions.nil?
60
+ @actions = ActionList.new
61
+
62
+ self.class.action_blocks.each do |block|
63
+ options = instance_eval(&block)
64
+ next if options.nil?
65
+
66
+ @actions << Action.new(options)
67
+ end
68
+
69
+ @actions
70
+ end
71
+
58
72
  module ClassMethods
59
- def action(name, path = nil, icon: nil); end
73
+ def action_blocks
74
+ @action_blocks || []
75
+ end
76
+
77
+ def add_action(&block)
78
+ @action_blocks ||= []
79
+ @action_blocks << block
80
+ end
60
81
  end
61
82
  end
@@ -0,0 +1,35 @@
1
+ module PassionView::Delegatable
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def delegate_prefix(*method_names)
6
+ @prefixes ||= {}
7
+ @prefixes["#{method_names.join('_')}_"] = method_names
8
+ end
9
+
10
+ def prefixes
11
+ @prefixes || {}
12
+ end
13
+ end
14
+
15
+ def method_missing(m, *args, &block)
16
+ self.class.prefixes.each do |prefix, method_names|
17
+ next unless m.to_s.start_with?(prefix)
18
+
19
+ last_m = m.to_s.gsub(prefix, '').to_sym
20
+ return method_names.inject(resource) do |result, method|
21
+ break if result.nil?
22
+
23
+ result.send method
24
+ end&.send(last_m)
25
+ end
26
+
27
+ return resource.send(m, *args, &block) if resource.respond_to?(m)
28
+
29
+ super
30
+ end
31
+
32
+ def respond_to_missing?(m, _)
33
+ resource.respond_to?(m)
34
+ end
35
+ end
@@ -1,8 +1,9 @@
1
1
  class PassionView::Filter::Base < PassionView::ViewModel::Base
2
- def initialize(name, options = {})
3
- super
2
+ def initialize(name, options = {}, &block)
3
+ super(name, options)
4
4
  @name = name
5
5
  @value = options[:value]
6
+ @block = block
6
7
  end
7
8
 
8
9
  def skip?
@@ -10,4 +11,8 @@ class PassionView::Filter::Base < PassionView::ViewModel::Base
10
11
  end
11
12
 
12
13
  attr_reader :name, :value
14
+
15
+ def apply(items)
16
+ return @block.call(items, value) if @block
17
+ end
13
18
  end
@@ -1,10 +1,12 @@
1
1
  class PassionView::Filter::SimpleFilter < PassionView::Filter::Base
2
- def initialize(name, field:, value: [])
2
+ def initialize(name, field:, value: [], &block)
3
3
  super
4
4
  @field = field
5
5
  end
6
6
 
7
7
  def apply(items)
8
+ super
9
+
8
10
  items.where(field => value)
9
11
  end
10
12
 
@@ -3,19 +3,29 @@ module PassionView::Filterable
3
3
 
4
4
  def initialize(items, options = {})
5
5
  super
6
- filters = options.delete(:filters) || {}
6
+ filters = filters_default.merge(options.delete(:filters) || {})
7
7
 
8
8
  filter_with(filters)
9
9
  end
10
10
 
11
11
  def items
12
- filters.reduce(super) { |items, filter| filter.skip? ? items : filter.apply(items) }
12
+ filters.reduce(super) do |items, filter|
13
+ filter.skip? ? items : filter.apply(items)
14
+ end
15
+ end
16
+
17
+ def filter_path
18
+ url_options
13
19
  end
14
20
 
15
21
  attr_reader :filters
16
22
 
17
23
  private
18
24
 
25
+ def filters_default
26
+ {}
27
+ end
28
+
19
29
  def url_options
20
30
  super.merge(filterable_params)
21
31
  end
@@ -32,10 +42,6 @@ module PassionView::Filterable
32
42
  extend ActiveSupport::Concern
33
43
 
34
44
  def filter_params
35
- action = params[:action]&.to_sym
36
-
37
- raise "Unfilterable action: #{action.inspect}" unless self.class.filter?(action)
38
-
39
45
  params[:filters]&.to_unsafe_h || {}
40
46
  end
41
47
 
@@ -43,20 +49,12 @@ module PassionView::Filterable
43
49
  def filter(*actions)
44
50
  filtered_actions.push(*actions)
45
51
  end
46
-
47
- def filter?(action)
48
- filtered_actions.include?(action)
49
- end
50
-
51
- def filtered_actions
52
- @filtered_actions ||= []
53
- end
54
52
  end
55
53
  end
56
54
 
57
55
  class FilterList < Array
58
56
  def present
59
- reject { |f| f.skip? }
57
+ reject(&:skip?)
60
58
  end
61
59
  end
62
60
  end
@@ -2,6 +2,10 @@ class PassionView::Form::Base < PassionView::ViewModel::Base
2
2
  include ActiveModel::Conversion
3
3
  include ActiveModel::Validations
4
4
 
5
+ def self.i18n_scope
6
+ :forms
7
+ end
8
+
5
9
  Delegation = Struct.new(
6
10
  :to,
7
11
  :prefix,
@@ -19,7 +19,7 @@ module PassionView::Paginable
19
19
  .per(current_per)
20
20
  end
21
21
 
22
- def next_pages(count = 2, &block)
22
+ def next_pages(count = 2)
23
23
  (-count..count).each do |idx|
24
24
  target = current_page + idx
25
25
  yield target, next_page_path(idx) if block_given? && !out_of_range?(target)
@@ -85,7 +85,7 @@ module PassionView::Paginable
85
85
  end
86
86
 
87
87
  def page_path(options = {})
88
- resource_path(options)
88
+ options
89
89
  end
90
90
 
91
91
  def url_options
@@ -112,25 +112,7 @@ module PassionView::Paginable
112
112
  extend ActiveSupport::Concern
113
113
 
114
114
  def pagination_params
115
- action = params[:action]&.to_sym
116
-
117
- raise "Unpaginable action: #{action.inspect}" unless self.class.paginate?(action)
118
-
119
115
  params.permit(:page, :per).to_h
120
116
  end
121
-
122
- module ClassMethods
123
- def paginate(*actions)
124
- paginated_actions.push(*actions)
125
- end
126
-
127
- def paginate?(action)
128
- paginated_actions.include?(action)
129
- end
130
-
131
- def paginated_actions
132
- @paginated_actions ||= []
133
- end
134
- end
135
117
  end
136
118
  end
@@ -0,0 +1,8 @@
1
+ class PassionView::MyRailtie < Rails::Railtie
2
+ config.to_prepare do
3
+ PassionView::Routable::RouteContext.instance_eval do
4
+ include Rails.application.routes.url_helpers
5
+ include Rails.application.routes.mounted_helpers
6
+ end
7
+ end
8
+ end
@@ -24,35 +24,19 @@ module PassionView::Relation
24
24
  item_view_model(items.first)
25
25
  end
26
26
 
27
- def count
28
- items.count
29
- end
27
+ delegate :count, to: :items
30
28
 
31
- def size
32
- items.size
33
- end
29
+ delegate :size, to: :items
34
30
 
35
- def include?(obj)
36
- items.include?(obj)
37
- end
31
+ delegate :include?, to: :items
38
32
 
39
- def empty?
40
- items.empty?
41
- end
33
+ delegate :empty?, to: :items
42
34
 
43
- def to_sql
44
- items.to_sql
45
- end
35
+ delegate :to_sql, to: :items
46
36
 
47
- def offset_value
48
- items.offset_value
49
- end
37
+ delegate :offset_value, to: :items
50
38
 
51
- def total_count
52
- items.total_count
53
- end
39
+ delegate :total_count, to: :items
54
40
 
55
- def limit_value
56
- items.limit_value
57
- end
41
+ delegate :limit_value, to: :items
58
42
  end
@@ -10,25 +10,11 @@ module PassionView::Resourceful
10
10
  @parent_resource = options[:parent]
11
11
  end
12
12
 
13
- def resource_path(options = {})
14
- routes.polymorphic_path(polymorphic_components, options)
15
- end
16
-
17
- private
18
-
19
13
  def resource
20
14
  @resource
21
15
  end
22
16
 
23
- def polymorphic_components
24
- options[:polymorphic_path] || [@parent_resource, nested_resource_path].compact
25
- end
26
-
27
17
  def resource_class
28
18
  @resource.klass #### @objects
29
19
  end
30
-
31
- def nested_resource_path
32
- resource_class
33
- end
34
20
  end
@@ -1,42 +1,43 @@
1
1
  # Routable exposes contextualized routes in an object
2
2
  module PassionView::Routable
3
3
  extend ActiveSupport::Concern
4
+ attr_reader :controller
4
5
 
5
- def contextualize_routes(obj)
6
- routes.contextualize(obj)
6
+ def contextualize_routes(controller)
7
+ @controller = controller
8
+ routes.contextualize(controller)
7
9
  end
8
10
 
9
11
  def routes
10
- @_routes_context ||= Class.new do
11
- include Rails.application.routes.url_helpers
12
- include Rails.application.routes.mounted_helpers
13
-
14
- def default_url_options
15
- @_context ? @_context.default_url_options : {}
16
- end
17
-
18
- def _routes_context
19
- self
20
- end
21
-
22
- def _with_routes(routes)
23
- old_routes, @_routes = @_routes, routes
24
- yield
25
- ensure
26
- @_routes = old_routes
27
- end
28
-
29
- def contextualized?
30
- !@_context.nil?
31
- end
32
-
33
- def contextualize(obj)
34
- @_context = obj
35
- end
36
-
37
- def without_context
38
- self.class.new
39
- end
40
- end.new
12
+ @_routes_context ||= RouteContext.new
13
+ end
14
+
15
+ class RouteContext
16
+ def default_url_options
17
+ @_context ? @_context.default_url_options : {}
18
+ end
19
+
20
+ def _routes_context
21
+ self
22
+ end
23
+
24
+ def _with_routes(routes)
25
+ old_routes, @_routes = @_routes, routes
26
+ yield
27
+ ensure
28
+ @_routes = old_routes
29
+ end
30
+
31
+ def contextualized?
32
+ !@_context.nil?
33
+ end
34
+
35
+ def contextualize(obj)
36
+ @_context = obj
37
+ end
38
+
39
+ def without_context
40
+ self.class.new
41
+ end
41
42
  end
42
43
  end
@@ -79,25 +79,7 @@ module PassionView::Sortable
79
79
  extend ActiveSupport::Concern
80
80
 
81
81
  def sort_params
82
- action = params[:action]&.to_sym
83
-
84
- raise "Unsortable action: #{action.inspect}" unless self.class.sort?(action)
85
-
86
82
  params.permit(:sort, :direction).to_h
87
83
  end
88
-
89
- module ClassMethods
90
- def sort(*actions)
91
- sorted_actions.push(*actions)
92
- end
93
-
94
- def sort?(action)
95
- sorted_actions.include?(action)
96
- end
97
-
98
- def sorted_actions
99
- @sorted_actions ||= []
100
- end
101
- end
102
84
  end
103
85
  end
@@ -7,7 +7,7 @@ class PassionView::ViewModel::Base
7
7
  end
8
8
 
9
9
  def self.i18n_scope
10
- :viewmodel
10
+ :cards
11
11
  end
12
12
 
13
13
  def self.lookup_ancestors
@@ -18,7 +18,11 @@ module PassionView::ViewModel::List
18
18
  end
19
19
 
20
20
  def item_view_model(item)
21
- item_view_model_class.new(item)
21
+ card = item_view_model_class.new(item)
22
+
23
+ card.contextualize_auth(user)
24
+ card.contextualize_routes(@controller) if @controller
25
+ card
22
26
  end
23
27
 
24
28
  private
data/lib/passion_view.rb CHANGED
@@ -13,3 +13,5 @@ require 'passion_view/view_model'
13
13
  require 'passion_view/card'
14
14
  require 'passion_view/form'
15
15
  require 'passion_view/filter'
16
+ require 'passion_view/delegatable'
17
+ require 'passion_view/railstie'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passionview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ADHOC-GTI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-21 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.46.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.46.0
13
27
  description: |2
14
28
  Get view logic out of persistence models and controllers and put it into
15
29
  view models.
@@ -25,6 +39,7 @@ files:
25
39
  - lib/passion_view/card.rb
26
40
  - lib/passion_view/card/base.rb
27
41
  - lib/passion_view/card/collection.rb
42
+ - lib/passion_view/delegatable.rb
28
43
  - lib/passion_view/filter.rb
29
44
  - lib/passion_view/filter/base.rb
30
45
  - lib/passion_view/filter/simple_filter.rb
@@ -33,6 +48,7 @@ files:
33
48
  - lib/passion_view/form/base.rb
34
49
  - lib/passion_view/paginable.rb
35
50
  - lib/passion_view/pathable.rb
51
+ - lib/passion_view/railstie.rb
36
52
  - lib/passion_view/relation.rb
37
53
  - lib/passion_view/resourceful.rb
38
54
  - lib/passion_view/routable.rb
@@ -61,9 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
77
  version: '0'
62
78
  requirements: []
63
79
  rubyforge_project:
64
- rubygems_version: 2.6.14.1
80
+ rubygems_version: 2.7.6
65
81
  signing_key:
66
82
  specification_version: 4
67
83
  summary: Love your views
68
84
  test_files: []
69
- has_rdoc: