diff --git a/Gemfile.lock b/Gemfile.lock index 9119e2fe..2b9ddfd2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -329,6 +329,7 @@ GEM PLATFORMS arm64-darwin-22 + arm64-darwin-23 x86_64-darwin-22 x86_64-darwin-23 x86_64-linux diff --git a/app/controllers/concerns/gem_class_scoped.rb b/app/controllers/concerns/gem_class_scoped.rb deleted file mode 100644 index a64aecf9..00000000 --- a/app/controllers/concerns/gem_class_scoped.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module GemClassScoped - extend ActiveSupport::Concern - - included do - before_action :set_class, except: :index - end - - private - - def set_class - @class = @gem.find_class!(params[:id]) - end -end diff --git a/app/controllers/concerns/gem_file_scoped.rb b/app/controllers/concerns/gem_file_scoped.rb deleted file mode 100644 index 5346ac13..00000000 --- a/app/controllers/concerns/gem_file_scoped.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -module GemFileScoped - extend ActiveSupport::Concern - - included do - before_action :set_file - end - - private - - def set_file - SourceFile.new(file: params[:id], gem: @gem).tap do |file| - @file = file if file.exist? - end - end -end diff --git a/app/controllers/concerns/gem_module_scoped.rb b/app/controllers/concerns/gem_module_scoped.rb deleted file mode 100644 index fac258bf..00000000 --- a/app/controllers/concerns/gem_module_scoped.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -module GemModuleScoped - extend ActiveSupport::Concern - - included do - before_action :set_module, except: :index - end - - private - - def set_module - @module = @gem.find_module!(params[:id]) - end -end diff --git a/app/controllers/concerns/gem_scoped.rb b/app/controllers/concerns/gem_scoped.rb deleted file mode 100644 index d2afad86..00000000 --- a/app/controllers/concerns/gem_scoped.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -module GemScoped - extend ActiveSupport::Concern - - included do - before_action :set_gem - - rescue_from "GemNotFoundError", "Gems::NotFound" do |exception| - redirect_to gems_path, notice: exception.message - end - - rescue_from "GemConstantNotFoundError" do |exception| - redirect_to gem_version_gem_path(@gem.name, @gem.version), notice: exception.message - end - end - - private - - def set_gem - if params[:version] - @gem = GemSpec.find(params[:gem], params[:version]) || raise(GemNotFoundError.new(params[:gem], params[:version])) - else - version = GemSpec.latest_version_for(params[:gem]) - gem = version ? GemSpec.find(params[:gem], version) : nil - - @gem = gem || raise(GemNotFoundError.new(params[:gem], version)) - end - end -end diff --git a/app/controllers/concerns/gem_target_scoped.rb b/app/controllers/concerns/gem_target_scoped.rb deleted file mode 100644 index 89364547..00000000 --- a/app/controllers/concerns/gem_target_scoped.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -module GemTargetScoped - extend ActiveSupport::Concern - - included do - before_action :set_target - end - - private - - def set_target - if params[:class_id] - @target = @gem.find_class!(params[:class_id]) - @namespace = @gem.find_namespace(@target.qualified_namespace) - elsif params[:module_id] - @target = @gem.find_module!(params[:module_id]) - else - @target = @gem.info.analyzer - end - end -end diff --git a/app/controllers/gems/base_controller.rb b/app/controllers/gems/base_controller.rb new file mode 100644 index 00000000..df1790bc --- /dev/null +++ b/app/controllers/gems/base_controller.rb @@ -0,0 +1,33 @@ +class Gems::BaseController < ApplicationController + before_action :set_gem + def self.set_target(...) = before_action(:set_target, ...) + + rescue_from GemNotFoundError, Gems::NotFound do |exception| + redirect_to gems_path, notice: exception.message + end + + rescue_from GemConstantNotFoundError do |exception| + redirect_to gem_version_gem_path(@gem.name, @gem.version), notice: exception.message + end + + private + + def set_gem + if gem = params[:gem] + version = perams[:version] || GemSpec.latest_version_for(gem) + @gem = GemSpec.find(gem, version) or raise GemNotFoundError.new(gem, version) + end + end + + def set_target + @target = + case + when params[:class_id] then @gem.find_class!(params[:class_id]) + when params[:module_id] then @gem.find_module!(params[:module_id]) + else + @gem.info.analyzer + end + + @namespace = @gem.find_namespace(@target.qualified_namespace) if params[:class_id] + end +end diff --git a/app/controllers/gems/class_methods_controller.rb b/app/controllers/gems/class_methods_controller.rb index b030d3d8..196b08cf 100644 --- a/app/controllers/gems/class_methods_controller.rb +++ b/app/controllers/gems/class_methods_controller.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -class Gems::ClassMethodsController < ApplicationController - include GemScoped - include GemTargetScoped +class Gems::ClassMethodsController < Gems::BaseController + set_target def show @method = @target.class_methods.find { |class_method| class_method.name == params[:id] } diff --git a/app/controllers/gems/classes_controller.rb b/app/controllers/gems/classes_controller.rb index 1d085ace..357754c2 100644 --- a/app/controllers/gems/classes_controller.rb +++ b/app/controllers/gems/classes_controller.rb @@ -1,13 +1,12 @@ # frozen_string_literal: true -class Gems::ClassesController < ApplicationController - include GemScoped - include GemClassScoped - +class Gems::ClassesController < Gems::BaseController def index end def show + @class = @gem.find_class!(params[:id]) + @classes = @gem.classes.select { |klass| klass.qualified_namespace == @class.qualified_name } @namespace = @gem.find_namespace(@class.qualified_namespace) end diff --git a/app/controllers/gems/docs_controller.rb b/app/controllers/gems/docs_controller.rb index ea4d21b9..d83d3f17 100644 --- a/app/controllers/gems/docs_controller.rb +++ b/app/controllers/gems/docs_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::DocsController < ApplicationController - include GemScoped - +class Gems::DocsController < Gems::BaseController def index end diff --git a/app/controllers/gems/files_controller.rb b/app/controllers/gems/files_controller.rb index e613310a..ace27b1a 100644 --- a/app/controllers/gems/files_controller.rb +++ b/app/controllers/gems/files_controller.rb @@ -1,12 +1,17 @@ # frozen_string_literal: true -class Gems::FilesController < ApplicationController - include GemScoped - include GemFileScoped +class Gems::FilesController < Gems::BaseController + before_action :set_file def index end def show end + + private + + def set_file + @file = SourceFile.new(gem: @gem, file: params[:id]).existent + end end diff --git a/app/controllers/gems/guides_controller.rb b/app/controllers/gems/guides_controller.rb index ba6876b6..c4839bca 100644 --- a/app/controllers/gems/guides_controller.rb +++ b/app/controllers/gems/guides_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::GuidesController < ApplicationController - include GemScoped - +class Gems::GuidesController < Gems::BaseController def index end diff --git a/app/controllers/gems/instance_methods_controller.rb b/app/controllers/gems/instance_methods_controller.rb index 76ee1a89..2ec6efb6 100644 --- a/app/controllers/gems/instance_methods_controller.rb +++ b/app/controllers/gems/instance_methods_controller.rb @@ -1,8 +1,7 @@ # frozen_string_literal: true -class Gems::InstanceMethodsController < ApplicationController - include GemScoped - include GemTargetScoped +class Gems::InstanceMethodsController < Gems::BaseController + set_target def show @method = @target.instance_methods.find { |instance_method| instance_method.name == params[:id] } diff --git a/app/controllers/gems/modules_controller.rb b/app/controllers/gems/modules_controller.rb index 41e01469..824f5cf7 100644 --- a/app/controllers/gems/modules_controller.rb +++ b/app/controllers/gems/modules_controller.rb @@ -1,13 +1,12 @@ # frozen_string_literal: true -class Gems::ModulesController < ApplicationController - include GemScoped - include GemModuleScoped - +class Gems::ModulesController < Gems::BaseController def index end def show + @module = @gem.find_module!(params[:id]) + @modules = @gem.modules.select { |mod| mod.qualified_namespace == @module.qualified_name } @classes = @gem.classes.select { |klass| klass.qualified_namespace == @module.qualified_name } end diff --git a/app/controllers/gems/pages_controller.rb b/app/controllers/gems/pages_controller.rb index 6c9cf39c..8a2aa462 100644 --- a/app/controllers/gems/pages_controller.rb +++ b/app/controllers/gems/pages_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::PagesController < ApplicationController - include GemScoped - +class Gems::PagesController < Gems::BaseController def show render params[:id].to_sym end diff --git a/app/controllers/gems/rbs_controller.rb b/app/controllers/gems/rbs_controller.rb index 912c4741..694f39d7 100644 --- a/app/controllers/gems/rbs_controller.rb +++ b/app/controllers/gems/rbs_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::RBSController < ApplicationController - include GemScoped - +class Gems::RBSController < Gems::BaseController def index require_samples = params[:require_samples].present? signature = @gem.rbs_signature(require_samples:) diff --git a/app/controllers/gems/search_controller.rb b/app/controllers/gems/search_controller.rb index dcde6a5b..359deff4 100644 --- a/app/controllers/gems/search_controller.rb +++ b/app/controllers/gems/search_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::SearchController < ApplicationController - include GemScoped - +class Gems::SearchController < Gems::BaseController def index query = params[:q].to_s.downcase diff --git a/app/controllers/gems/types_controller.rb b/app/controllers/gems/types_controller.rb index 8d07ba81..e20834ec 100644 --- a/app/controllers/gems/types_controller.rb +++ b/app/controllers/gems/types_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::TypesController < ApplicationController - include GemScoped - +class Gems::TypesController < Gems::BaseController def index @samples = Types::Sample .group(:gem_name, :gem_version, :receiver, :method_name) diff --git a/app/controllers/gems/versions_controller.rb b/app/controllers/gems/versions_controller.rb index a11c0e7d..c2316687 100644 --- a/app/controllers/gems/versions_controller.rb +++ b/app/controllers/gems/versions_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Gems::VersionsController < ApplicationController - include GemScoped - +class Gems::VersionsController < Gems::BaseController def index end end diff --git a/app/controllers/gems_controller.rb b/app/controllers/gems_controller.rb index 35951afd..15a62f70 100644 --- a/app/controllers/gems_controller.rb +++ b/app/controllers/gems_controller.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true -class GemsController < ApplicationController - include GemScoped - - before_action :set_gem, except: :index - +class GemsController < Gems::BaseController def index @gems = Gems.just_updated end diff --git a/app/models/source_file.rb b/app/models/source_file.rb index 1cb50ee7..98298a6d 100644 --- a/app/models/source_file.rb +++ b/app/models/source_file.rb @@ -13,6 +13,10 @@ def source_path "#{gem.unpack_data_path}/#{file}" end + def existent + self if exist? + end + def exist? file && gem.files.include?(file) && File.exist?(source_path) end diff --git a/config/database.yml b/config/database.yml index 8768b10b..f85a0f93 100644 --- a/config/database.yml +++ b/config/database.yml @@ -57,8 +57,6 @@ test: <<: *default database: gemsh_test host: localhost - username: postgres - password: postgres # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is