Commit 77b53086 by liyijie

feat: simple_controller 支持mongoid

parent 5e590694
...@@ -175,7 +175,13 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase ...@@ -175,7 +175,13 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
end end
def belongs_to_refs def belongs_to_refs
resource_class.reflections.values.select { |ref| ref.belongs_to? && !ref.polymorphic? } active_record? ?
resource_class.reflections.values.select { |ref| ref.belongs_to? && !ref.polymorphic? } :
[]
end
def active_record?
resource_class < ActiveRecord::Base
end end
def filename_with_extensions(name) def filename_with_extensions(name)
......
<%- if active_record? -%>
json.extract!( json.extract!(
<%= resource_singular %>, <%= resource_singular %>,
:id, :id,
...@@ -7,3 +8,6 @@ json.extract!( ...@@ -7,3 +8,6 @@ json.extract!(
:<%= attribute_name %>, :<%= attribute_name %>,
<%- end -%> <%- end -%>
) )
<%- else -%>
json.merge! <%= resource_singular %>.as_json
<%- end -%>
...@@ -5,6 +5,7 @@ require 'responders' ...@@ -5,6 +5,7 @@ require 'responders'
require 'ransack' require 'ransack'
require 'inherited_resources' require 'inherited_resources'
require 'pundit' require 'pundit'
require 'ransack_mongo'
module SimpleController module SimpleController
autoload :VERSION, 'simple_controller/version' autoload :VERSION, 'simple_controller/version'
......
...@@ -211,7 +211,7 @@ class SimpleController::BaseController < ::InheritedResources::Base ...@@ -211,7 +211,7 @@ class SimpleController::BaseController < ::InheritedResources::Base
association = association.ransack(params[:q]).result unless self.class.instance_variable_get(:@ransack_off) || params[:q].blank? association = association.ransack(params[:q]).result unless self.class.instance_variable_get(:@ransack_off) || params[:q].blank?
association = association.ransack(params[:sub_q]).result unless self.class.instance_variable_get(:@ransack_off) || params[:sub_q].blank? association = association.ransack(params[:sub_q]).result unless self.class.instance_variable_get(:@ransack_off) || params[:sub_q].blank?
association = association.distinct unless self.class.instance_variable_get(:@distinct_off) || !association.respond_to?(:distinct) association = association.distinct unless self.class.instance_variable_get(:@distinct_off) || !association.respond_to?(:distinct) || !active_record?
association = association.paginate(page: params[:page], per_page: params[:per_page]) unless self.class.instance_variable_get(:@paginate_off) association = association.paginate(page: params[:page], per_page: params[:per_page]) unless self.class.instance_variable_get(:@paginate_off)
association association
end end
...@@ -225,7 +225,7 @@ class SimpleController::BaseController < ::InheritedResources::Base ...@@ -225,7 +225,7 @@ class SimpleController::BaseController < ::InheritedResources::Base
origin_end_of_association_chain.is_a?(ActiveRecord::Relation) origin_end_of_association_chain.is_a?(ActiveRecord::Relation)
scope_policy_class.new(current_user, origin_end_of_association_chain).resolve scope_policy_class.new(current_user, origin_end_of_association_chain).resolve
else else
origin_end_of_association_chain origin_end_of_association_chain.all
end end
end end
...@@ -281,7 +281,7 @@ class SimpleController::BaseController < ::InheritedResources::Base ...@@ -281,7 +281,7 @@ class SimpleController::BaseController < ::InheritedResources::Base
association = Array(params[:q][:scopes]).reduce(association) { |_association, _scope| _association.send(_scope) } if params[:q][:scopes].present? association = Array(params[:q][:scopes]).reduce(association) { |_association, _scope| _association.send(_scope) } if params[:q][:scopes].present?
association = association.ransack(params[:q].except(:scopes)).result association = association.ransack(params[:q].except(:scopes)).result
end end
association = association.distinct unless self.class.instance_variable_get(:@distinct_off) || !association.respond_to?(:distinct) association = association.distinct unless self.class.instance_variable_get(:@distinct_off) || !association.respond_to?(:distinct)|| !active_record?
association association
end end
...@@ -319,4 +319,8 @@ class SimpleController::BaseController < ::InheritedResources::Base ...@@ -319,4 +319,8 @@ class SimpleController::BaseController < ::InheritedResources::Base
authorize(record, query, policy_class: policy_class) : authorize(record, query, policy_class: policy_class) :
record record
end end
def active_record?
self.class.resource_class < ActiveRecord::Base
end
end end
...@@ -35,6 +35,7 @@ Gem::Specification.new do |spec| ...@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "will_paginate" spec.add_dependency "will_paginate"
spec.add_dependency "responders" spec.add_dependency "responders"
spec.add_dependency "pundit" spec.add_dependency "pundit"
spec.add_dependency "ransack_mongo"
spec.add_development_dependency "bundler" spec.add_development_dependency "bundler"
spec.add_development_dependency "rake" spec.add_development_dependency "rake"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment