Commit 77b53086 by liyijie

feat: simple_controller 支持mongoid

parent 5e590694
......@@ -175,7 +175,13 @@ class SimpleControllerGenerator < Rails::Generators::NamedBase
end
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
def filename_with_extensions(name)
......
<%- if active_record? -%>
json.extract!(
<%= resource_singular %>,
:id,
......@@ -7,3 +8,6 @@ json.extract!(
:<%= attribute_name %>,
<%- end -%>
)
<%- else -%>
json.merge! <%= resource_singular %>.as_json
<%- end -%>
......@@ -5,6 +5,7 @@ require 'responders'
require 'ransack'
require 'inherited_resources'
require 'pundit'
require 'ransack_mongo'
module SimpleController
autoload :VERSION, 'simple_controller/version'
......
......@@ -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[: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
end
......@@ -225,7 +225,7 @@ class SimpleController::BaseController < ::InheritedResources::Base
origin_end_of_association_chain.is_a?(ActiveRecord::Relation)
scope_policy_class.new(current_user, origin_end_of_association_chain).resolve
else
origin_end_of_association_chain
origin_end_of_association_chain.all
end
end
......@@ -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 = association.ransack(params[:q].except(:scopes)).result
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
end
......@@ -319,4 +319,8 @@ class SimpleController::BaseController < ::InheritedResources::Base
authorize(record, query, policy_class: policy_class) :
record
end
def active_record?
self.class.resource_class < ActiveRecord::Base
end
end
......@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "will_paginate"
spec.add_dependency "responders"
spec.add_dependency "pundit"
spec.add_dependency "ransack_mongo"
spec.add_development_dependency "bundler"
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