-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support resource-based view lookups. (#2)
* update readme to explain goal * views are proven to work * 2.3.1 ruby travis * before_script * respond_to do format * something's not right * progress * views work now, but lookup of models is now broken * all tests pass * rubocop -a
- Loading branch information
1 parent
66d74f2
commit 24ded1c
Showing
69 changed files
with
381 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# frozen_string_literal: true | ||
require 'bundler/gem_tasks' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
lib/rails_module_unification/action_view/path_extensions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
module RailsModuleUnification | ||
# prepend view paths, setting preferential lookup to the new | ||
# RMU folders | ||
# | ||
# lookup pattern | ||
# resources/:namespace/:resource/views/:action/{.:locale,}{.:formats,}{+:variants,}{.:handlers,} | ||
# prefix = resources/:namespace/:resource/views/ | ||
# | ||
# default lookup pattern (for reference (as of 5.0.0.1)) | ||
# :prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,} | ||
# | ||
# This module should only be used as class methods on the inheriting object | ||
module PathExtensions | ||
require 'awesome_print' | ||
|
||
def local_prefixes | ||
[_rmu_resource_path] + super | ||
end | ||
|
||
private | ||
|
||
def _rmu_resource_path | ||
[ | ||
_namespace, | ||
_resource_name, | ||
'views' | ||
].flatten.reject(&:blank?).map(&:underscore).join('/') | ||
end | ||
|
||
def _resource_name | ||
controller_name | ||
end | ||
|
||
def _namespace | ||
_resource_parts.namespace | ||
end | ||
|
||
def _resource_parts | ||
@_resource_parts ||= RailsModuleUnification::ResourceParts.call(name) | ||
end | ||
end | ||
end |
44 changes: 44 additions & 0 deletions
44
lib/rails_module_unification/action_view/resource_resolver.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
module RailsModuleUnification | ||
class ResourceResolver < ::ActionView::OptimizedFileSystemResolver | ||
require 'pry-byebug' | ||
def initialize | ||
path = [ | ||
Rails.root, | ||
'app', | ||
RailsModuleUnification.directory, | ||
'resources' | ||
].reject(&:blank?).join('/') | ||
|
||
super(path) | ||
@path = path | ||
end | ||
|
||
# def find_templates(name, prefix, partial, details) | ||
# binding.pry | ||
# super(name, prefix, partial, details) | ||
# end | ||
# | ||
# def build_query(path, details) | ||
# | ||
# query = @pattern.dup | ||
# | ||
# binding.pry | ||
# prefix = path.prefix.empty? ? '' : "#{escape_entry(path.prefix)}\\1" | ||
# query.gsub!(/:prefix(\/)?/, prefix) | ||
# | ||
# partial = escape_entry(path.partial? ? "_#{path.name}" : path.name) | ||
# query.gsub!(/:action/, partial) | ||
# | ||
# details.each do |ext, candidates| | ||
# if ext == :variants && candidates == :any | ||
# query.gsub!(/:#{ext}/, "*") | ||
# else | ||
# query.gsub!(/:#{ext}/, "{#{candidates.compact.uniq.join(',')}}") | ||
# end | ||
# end | ||
# puts File.expand_path(query, @path) | ||
# File.expand_path(query, @path) | ||
# end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# frozen_string_literal: true | ||
module RailsModuleUnification | ||
class ResourceParts | ||
RESOURCE_SUFFIX_NAMES = RailsModuleUnification::DependencyExtensions::RESOURCE_SUFFIX_NAMES | ||
QUALIFIED_NAME_SPLIT = RailsModuleUnification::DependencyExtensions::QUALIFIED_NAME_SPLIT | ||
|
||
attr_reader :namespace, :resource_name, | ||
:resource_type, :named_resource_type, | ||
:class_path | ||
|
||
class << self | ||
def from_name(name) | ||
resource = call(name) | ||
|
||
[ | ||
resource.namespace, | ||
resource.resource_name, | ||
resource.resource_type, | ||
resource.named_resource_type, | ||
resource.class_path | ||
] | ||
end | ||
|
||
def call(name) | ||
resource = new(name) | ||
resource.call | ||
resource | ||
end | ||
end | ||
|
||
def initialize(name) | ||
@qualified_name = name | ||
end | ||
|
||
def call | ||
# if this is not part of a resource, don't even bother | ||
return unless index_of_resource_type | ||
|
||
# Api, V2, Post, Operations, Update | ||
# => Operations | ||
@resource_type = qualified_parts[index_of_resource_type] | ||
|
||
# Api, V2, Post, Operations, Update | ||
# => Posts | ||
# | ||
# Posts, Controller | ||
# => Posts | ||
original_resource_name = qualified_parts[index_of_resource_type - 1] | ||
@resource_name = original_resource_name.pluralize | ||
|
||
# Posts_Controller | ||
# Post_Operations | ||
@named_resource_type = "#{original_resource_name}_#{@resource_type}" | ||
|
||
# Api, V2, Post, Operations, Update | ||
# => Api, V2 | ||
namespace_index = index_of_resource_type - 1 | ||
@namespace = namespace_index < 1 ? '' : qualified_parts.take(namespace_index) | ||
|
||
# Api, V2, Post, Operations, Update | ||
# => Update | ||
class_index = index_of_resource_type + 1 | ||
@class_path = class_index < 1 ? '' : qualified_parts.drop(class_index) | ||
end | ||
|
||
private | ||
|
||
# 1. break apart the qualified name into pieces that can easily be | ||
# manipulated | ||
# | ||
# Api::Posts | ||
# => Api, Posts | ||
# | ||
# Api::PostOperations::Create | ||
# => Api, Post, Operations, Create | ||
# | ||
# Api::PostsController | ||
# => Api, Posts, Controller | ||
# | ||
# Api::V2::PostOperations::Update | ||
# => Api, V2, Post, Operations, Update | ||
def qualified_parts | ||
@qualified_parts ||= @qualified_name.split(QUALIFIED_NAME_SPLIT).reject(&:blank?) | ||
end | ||
|
||
# based on the position of of the resource type name, | ||
# anything to the left will be the namespace, and anything | ||
# to the right will be the file path within the namespace | ||
# (may be obvious, but basically, we're 'pivoting' on RESOURCE_SUFFIX_NAMES) | ||
# | ||
# Given: Api, V2, Post, Operations, Update | ||
# ^ index_of_resource_type (3) | ||
def index_of_resource_type | ||
@index_of_resource_type ||= qualified_parts.index { |x| RESOURCE_SUFFIX_NAMES.include?(x) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# frozen_string_literal: true | ||
module RailsModuleUnification | ||
VERSION = '0.6.1'.freeze | ||
VERSION = '0.6.1' | ||
end |
Oops, something went wrong.