rails_module_unification 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc84fe5532bfbf7474b6ef8ca641c7d674b46dec
4
- data.tar.gz: 8c9a6e0d21a1e89fe0cc047efb8ba8c6936f7db4
3
+ metadata.gz: 242883465990fa99744ab5d34a36edbb481b404a
4
+ data.tar.gz: 77eeaecbb970da8cd11b089791cadc7bd8d1cf51
5
5
  SHA512:
6
- metadata.gz: f0490b4d9fea09c359fe7b6b47aa184261c16c27939468a1f2baf6582f226d56ed9541ba376c784253f55452a90d8d464b948c7701fe72beaf7dc9a496b41bd3
7
- data.tar.gz: 0ccfb828d9053a8f221571a2a668dc23dcfc47b1f18013bab3796ff7401157ce93d38b4a1b7688dc7133c25fe84c92f5a28ee0d77f6442d762803a58e434fe89
6
+ metadata.gz: 96c5446792bc5a2f236c06ef57e991e686894a0d8258961bafc3d177b06161172e7cab5cf2c263725bf4aff58d9243b04f2fa83b453851adfac1de3f2c339711
7
+ data.tar.gz: 3d2d4e0b01b3f04038ac2876b4a4d05e6d6b06b4014a81f27ed2f06091194f93884fd2aa5758da01b1b40f50df5c088df9aebbecdba308ea02c2a5f75469243b
data/README.md CHANGED
@@ -19,10 +19,11 @@ This gem provides a way to re-structure your app so that like-objects are groupe
19
19
  ```
20
20
  app/
21
21
  ├── channels/
22
- ├── data/
23
- └── models/
24
- ├── post.rb
25
- ├── comment.rb
22
+ ├── models/
23
+ ├── data/
24
+ ├── post.rb
25
+ │ └── comment.rb
26
+ │ └── graph_data.rb
26
27
  ├── jobs/
27
28
  ├── mailers/
28
29
  │ └── notification_mailer.rb
@@ -34,7 +35,10 @@ app/
34
35
  │ └── serializer.rb
35
36
  └── comments/
36
37
  ├── controller.rb
37
- └── serializer.rb
38
+ ├── serializer.rb
39
+ └── views/
40
+ ├── index.html.erb
41
+ └── create.html.erb
38
42
 
39
43
  ```
40
44
 
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support'
4
- require 'rails_module_unification/active_support_extensions'
5
4
 
6
5
  module RailsModuleUnification
7
- extend ActiveSupport::Autoload
6
+ require 'rails_module_unification/active_support/dependency_extensions'
7
+ require 'rails_module_unification/action_view/path_extensions'
8
+ require 'rails_module_unification/action_view/resource_resolver'
9
+ require 'rails_module_unification/resource_parts'
8
10
 
9
11
  module_function
10
12
 
@@ -17,5 +19,7 @@ module RailsModuleUnification
17
19
  end
18
20
 
19
21
  require 'rails_module_unification/railtie'
20
- ActiveSupport::Dependencies.extend RailsModuleUnification::ActiveSupportExtensions
22
+ ActiveSupport::Dependencies.extend RailsModuleUnification::DependencyExtensions
23
+ ActionController::Base.extend RailsModuleUnification::PathExtensions
24
+ ActionController::API.extend RailsModuleUnification::PathExtensions
21
25
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ module RailsModuleUnification
3
+ # prepend view paths, setting preferential lookup to the new
4
+ # RMU folders
5
+ #
6
+ # lookup pattern
7
+ # resources/:namespace/:resource/views/:action/{.:locale,}{.:formats,}{+:variants,}{.:handlers,}
8
+ # prefix = resources/:namespace/:resource/views/
9
+ #
10
+ # default lookup pattern (for reference (as of 5.0.0.1))
11
+ # :prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}
12
+ #
13
+ # This module should only be used as class methods on the inheriting object
14
+ module PathExtensions
15
+ require 'awesome_print'
16
+
17
+ def local_prefixes
18
+ [_rmu_resource_path] + super
19
+ end
20
+
21
+ private
22
+
23
+ def _rmu_resource_path
24
+ [
25
+ _namespace,
26
+ _resource_name,
27
+ 'views'
28
+ ].flatten.reject(&:blank?).map(&:underscore).join('/')
29
+ end
30
+
31
+ def _resource_name
32
+ controller_name
33
+ end
34
+
35
+ def _namespace
36
+ _resource_parts.namespace
37
+ end
38
+
39
+ def _resource_parts
40
+ @_resource_parts ||= RailsModuleUnification::ResourceParts.call(name)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ module RailsModuleUnification
3
+ class ResourceResolver < ::ActionView::OptimizedFileSystemResolver
4
+ require 'pry-byebug'
5
+ def initialize
6
+ path = [
7
+ Rails.root,
8
+ 'app',
9
+ RailsModuleUnification.directory,
10
+ 'resources'
11
+ ].reject(&:blank?).join('/')
12
+
13
+ super(path)
14
+ @path = path
15
+ end
16
+
17
+ # def find_templates(name, prefix, partial, details)
18
+ # binding.pry
19
+ # super(name, prefix, partial, details)
20
+ # end
21
+ #
22
+ # def build_query(path, details)
23
+ #
24
+ # query = @pattern.dup
25
+ #
26
+ # binding.pry
27
+ # prefix = path.prefix.empty? ? '' : "#{escape_entry(path.prefix)}\\1"
28
+ # query.gsub!(/:prefix(\/)?/, prefix)
29
+ #
30
+ # partial = escape_entry(path.partial? ? "_#{path.name}" : path.name)
31
+ # query.gsub!(/:action/, partial)
32
+ #
33
+ # details.each do |ext, candidates|
34
+ # if ext == :variants && candidates == :any
35
+ # query.gsub!(/:#{ext}/, "*")
36
+ # else
37
+ # query.gsub!(/:#{ext}/, "{#{candidates.compact.uniq.join(',')}}")
38
+ # end
39
+ # end
40
+ # puts File.expand_path(query, @path)
41
+ # File.expand_path(query, @path)
42
+ # end
43
+ end
44
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module RailsModuleUnification
3
- module ActiveSupportExtensions
3
+ module DependencyExtensions
4
4
  RESOURCE_SUFFIX_NAMES = %w(
5
5
  Controller
6
6
  Serializer
@@ -76,61 +76,11 @@ module RailsModuleUnification
76
76
  #
77
77
  # @param [String] qualified_name fully qualified class/module name to find the file location for
78
78
  def resource_path_from_qualified_name(qualified_name)
79
- # 1. break apart the qualified name into pieces that can easily be
80
- # manipulated
81
- #
82
- # Api::Posts
83
- # => Api, Posts
84
- #
85
- # Api::PostOperations::Create
86
- # => Api, Post, Operations, Create
87
- #
88
- # Api::PostsController
89
- # => Api, Posts, Controller
90
- #
91
- # Api::V2::PostOperations::Update
92
- # => Api, V2, Post, Operations, Update
93
- qualified_parts = qualified_name.split(QUALIFIED_NAME_SPLIT).reject(&:blank?)
94
-
95
- # based on the position of of the resource type name,
96
- # anything to the left will be the namespace, and anything
97
- # to the right will be the file path within the namespace
98
- # (may be obvious, but basically, we're 'pivoting' on RESOURCE_SUFFIX_NAMES)
99
- #
100
- # Given: Api, V2, Post, Operations, Update
101
- # ^ index_of_resource_type (3)
102
- index_of_resource_type = qualified_parts.index { |x| RESOURCE_SUFFIX_NAMES.include?(x) }
103
-
104
- # if this is not part of a resource, don't even bother
105
- return unless index_of_resource_type
106
-
107
- # Api, V2, Post, Operations, Update
108
- # => Operations
109
- resource_type = qualified_parts[index_of_resource_type]
110
-
111
- # Api, V2, Post, Operations, Update
112
- # => Posts
113
- #
114
- # Posts, Controller
115
- # => Posts
116
- original_resource_name = qualified_parts[index_of_resource_type - 1]
117
- resource_name = original_resource_name.pluralize
118
-
119
- # Posts_Controller
120
- # Post_Operations
121
- named_resource_type = "#{original_resource_name}_#{resource_type}"
122
-
123
- # Api, V2, Post, Operations, Update
124
- # => Api, V2
125
- namespace_index = index_of_resource_type - 1
126
- namespace = namespace_index < 1 ? '' : qualified_parts.take(namespace_index)
127
-
128
- # Api, V2, Post, Operations, Update
129
- # => Update
130
- class_index = index_of_resource_type + 1
131
- class_path = class_index < 1 ? '' : qualified_parts.drop(class_index)
132
-
133
- # Finally,
79
+ namespace,
80
+ resource_name,
81
+ resource_type, named_resource_type,
82
+ class_path = ResourceParts.from_name(qualified_name)
83
+
134
84
  # build all the possible places that this file could be
135
85
  path_options = [
136
86
 
@@ -149,7 +99,6 @@ module RailsModuleUnification
149
99
 
150
100
  file_path = ''
151
101
  path_options.each do |path_option|
152
-
153
102
  file_path = search_for_file(path_option)
154
103
 
155
104
  break if file_path.present?
@@ -9,20 +9,30 @@ module RailsModuleUnification
9
9
  load 'tasks/rails_module_unification.rake'
10
10
  end
11
11
 
12
+ # for customizing where the new folder structure is
13
+ # by default, everything still resides in Rails.root/app
12
14
  config_path = "#{Rails.root}/config/initializers/rails_module_unification"
13
15
  config_exists = File.exist?(config_path)
14
16
  require config_path if config_exists
15
17
 
18
+ # add folders to autoload paths
16
19
  initializer 'activeservice.autoload', before: :set_autoload_paths do |app|
17
- mu_dir = "#{Rails.root}/app/#{RailsModuleUnification.directory}"
20
+ mu_dir = [
21
+ Rails.root,
22
+ 'app',
23
+ RailsModuleUnification.directory
24
+ ].reject(&:blank?).join('/')
18
25
 
19
- # Data
20
- data_paths = Dir["#{mu_dir}/models/data/**/"]
21
- app.config.autoload_paths += data_paths
26
+ # New location for ActiveRecord Models
27
+ app.config.autoload_paths << "#{mu_dir}/models/data"
22
28
 
23
29
  # Resources
24
- resource_paths = Dir["#{mu_dir}/resources/"]
25
- app.config.autoload_paths += resource_paths
30
+ app.config.autoload_paths << "#{mu_dir}/resources/"
31
+ end
32
+
33
+ config.after_initialize do
34
+ # binding.pry
35
+ ActionController::Base.prepend_view_path RailsModuleUnification::ResourceResolver.new
26
36
  end
27
37
  end
28
38
  end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+ module RailsModuleUnification
3
+ class ResourceParts
4
+ RESOURCE_SUFFIX_NAMES = RailsModuleUnification::DependencyExtensions::RESOURCE_SUFFIX_NAMES
5
+ QUALIFIED_NAME_SPLIT = RailsModuleUnification::DependencyExtensions::QUALIFIED_NAME_SPLIT
6
+
7
+ attr_reader :namespace, :resource_name,
8
+ :resource_type, :named_resource_type,
9
+ :class_path
10
+
11
+ class << self
12
+ def from_name(name)
13
+ resource = call(name)
14
+
15
+ [
16
+ resource.namespace,
17
+ resource.resource_name,
18
+ resource.resource_type,
19
+ resource.named_resource_type,
20
+ resource.class_path
21
+ ]
22
+ end
23
+
24
+ def call(name)
25
+ resource = new(name)
26
+ resource.call
27
+ resource
28
+ end
29
+ end
30
+
31
+ def initialize(name)
32
+ @qualified_name = name
33
+ end
34
+
35
+ def call
36
+ # if this is not part of a resource, don't even bother
37
+ return unless index_of_resource_type
38
+
39
+ # Api, V2, Post, Operations, Update
40
+ # => Operations
41
+ @resource_type = qualified_parts[index_of_resource_type]
42
+
43
+ # Api, V2, Post, Operations, Update
44
+ # => Posts
45
+ #
46
+ # Posts, Controller
47
+ # => Posts
48
+ original_resource_name = qualified_parts[index_of_resource_type - 1]
49
+ @resource_name = original_resource_name.pluralize
50
+
51
+ # Posts_Controller
52
+ # Post_Operations
53
+ @named_resource_type = "#{original_resource_name}_#{@resource_type}"
54
+
55
+ # Api, V2, Post, Operations, Update
56
+ # => Api, V2
57
+ namespace_index = index_of_resource_type - 1
58
+ @namespace = namespace_index < 1 ? '' : qualified_parts.take(namespace_index)
59
+
60
+ # Api, V2, Post, Operations, Update
61
+ # => Update
62
+ class_index = index_of_resource_type + 1
63
+ @class_path = class_index < 1 ? '' : qualified_parts.drop(class_index)
64
+ end
65
+
66
+ private
67
+
68
+ # 1. break apart the qualified name into pieces that can easily be
69
+ # manipulated
70
+ #
71
+ # Api::Posts
72
+ # => Api, Posts
73
+ #
74
+ # Api::PostOperations::Create
75
+ # => Api, Post, Operations, Create
76
+ #
77
+ # Api::PostsController
78
+ # => Api, Posts, Controller
79
+ #
80
+ # Api::V2::PostOperations::Update
81
+ # => Api, V2, Post, Operations, Update
82
+ def qualified_parts
83
+ @qualified_parts ||= @qualified_name.split(QUALIFIED_NAME_SPLIT).reject(&:blank?)
84
+ end
85
+
86
+ # based on the position of of the resource type name,
87
+ # anything to the left will be the namespace, and anything
88
+ # to the right will be the file path within the namespace
89
+ # (may be obvious, but basically, we're 'pivoting' on RESOURCE_SUFFIX_NAMES)
90
+ #
91
+ # Given: Api, V2, Post, Operations, Update
92
+ # ^ index_of_resource_type (3)
93
+ def index_of_resource_type
94
+ @index_of_resource_type ||= qualified_parts.index { |x| RESOURCE_SUFFIX_NAMES.include?(x) }
95
+ end
96
+ end
97
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module RailsModuleUnification
3
- VERSION = '0.6.1'.freeze
3
+ VERSION = '0.7.0'
4
4
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  namespace :rmu do
2
3
  config_path = "#{Rails.root}/config/initializers/rails_module_unification"
3
4
  config_exists = File.exist?(config_path)
@@ -33,7 +34,6 @@ namespace :rmu do
33
34
 
34
35
  next if already_moved?(location)
35
36
 
36
-
37
37
  destination = destination_for(location)
38
38
  move_file(location, to: destination)
39
39
  end
@@ -102,12 +102,12 @@ namespace :rmu do
102
102
  Presenters
103
103
  Policy
104
104
  Policies
105
- )
105
+ ).freeze
106
106
 
107
107
  # PostsController
108
108
  PLURAL_RESOURCE_SUFFIXES = %w(
109
109
  Controller
110
- )
110
+ ).freeze
111
111
 
112
112
  def possible_classes(resource_name, plural: false)
113
113
  klass_name = plural ? resource_name.pluralize : resource_name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_module_unification
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - L. Preston Sego III
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-27 00:00:00.000000000 Z
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -159,8 +159,11 @@ files:
159
159
  - LICENSE
160
160
  - README.md
161
161
  - lib/rails_module_unification.rb
162
- - lib/rails_module_unification/active_support_extensions.rb
162
+ - lib/rails_module_unification/action_view/path_extensions.rb
163
+ - lib/rails_module_unification/action_view/resource_resolver.rb
164
+ - lib/rails_module_unification/active_support/dependency_extensions.rb
163
165
  - lib/rails_module_unification/railtie.rb
166
+ - lib/rails_module_unification/resource_parts.rb
164
167
  - lib/rails_module_unification/version.rb
165
168
  - lib/tasks/rails_module_unification.rake
166
169
  homepage: https://github.com/NullVoxPopuli/rails_module_unification
@@ -186,5 +189,5 @@ rubyforge_project:
186
189
  rubygems_version: 2.6.7
187
190
  signing_key:
188
191
  specification_version: 4
189
- summary: RailsModuleUnification-0.6.1
192
+ summary: RailsModuleUnification-0.7.0
190
193
  test_files: []