2017-05-16 8 views
0

내 앱에서 일부 리소스는 hippa와 호환되어야하며 필요에 맞게 ActiveAdmin :: Comments를 사용합니다. 필터링이 적용되지 않으면 인덱스 컬렉션을 숨 깁니다.일부 리소스에만 ActiveAdmin 리소스 컨트롤러 덮어 쓰기

모든 hippa 리소스는 일반적인 ActiveAdmin.register [리소스] 내에 총체적으로 작성되므로 모든 리소스에 여러 번 코드를 자르고 붙여 넣기보다는 다른 곳에서 기본 리소스 컨트롤러를 무시하고 이러한 특정 리소스를 HippaResource에서 가져 오게하는 방법이 있기를 바랬습니다. 또는 사용 된 제네릭 코드를 확장하거나 포함 할 수있는 방법이 있습니까?

답변

0

글쎄, 다시 한번 나는 내 자신의 질문에 대답 할 것이다. 솔직히 말해서, 비록 내가 알아 냈다고하더라도, 나는 이다. 더 쉬운 방법이 있어야한다.

는 기본적으로 본인은 설정/초기화/active_admin.rb 내에서 원하는 최우선 썼다 - 일반적으로 이미 존재하는 자원 컨트롤러를 대체 할,하지만 내가 원하는 것을하지 않았다 - 나는 일부 HIPPA의 commpliant 자원/resource_controllers를 원했다 특정 방식으로 작동 - 데이터의 색인 페이지를 숨기고 필터링 된 후에 만 ​​표시합니다.

module ActiveAdmin 
    module Views 
    class IndexAsHippa < ActiveAdmin::Component 

     def build(page_presenter, collection) 
     table_options = { 
      id: "hippa_index_table_#{active_admin_config.resource_name.plural}", 
      sortable: true, 
      class: "index_table index", 
      i18n: active_admin_config.resource_class, 
      paginator: page_presenter[:paginator] != false, 
      row_class: page_presenter[:row_class] 
     } 


     table_for collection, table_options do |t| 
      table_config_block = page_presenter.block || default_table 
      instance_exec(t, &table_config_block) 
     end 
     end 

     def table_for(*args, &block) 
     insert_tag IndexTableFor, *args, &block 
     end 

     def default_table 
     proc do 
      selectable_column 
      id_column if resource_class.primary_key 
      active_admin_config.resource_columns.each do |attribute| 
      column attribute 
      end 
      actions 
     end 
     end 

     def self.index_name 
     "hippa_table" 
     end 

     # 
     # Extend the default ActiveAdmin::Views::TableFor with some 
     # methods for quickly displaying items on the index page 
     # 
     class IndexTableFor < ::ActiveAdmin::Views::TableFor 

     # Display a column for checkbox 
     def selectable_column 
      return unless active_admin_config.batch_actions.any? 
      column resource_selection_toggle_cell, class: 'col-selectable', sortable: false do |resource| 
      resource_selection_cell resource 
      end 
     end 

     def index_column(start_value = 1) 
      column '#', class: 'col-index', sortable: false do |resource| 
      @collection.offset_value + @collection.index(resource) + start_value 
      end 
     end 

     # Display a column for the id 
     def id_column 
      raise "#{resource_class.name} has no primary_key!" unless resource_class.primary_key 
      column(resource_class.human_attribute_name(resource_class.primary_key), sortable: resource_class.primary_key) do |resource| 
      if controller.action_methods.include?('show') 
       link_to resource.id, resource_path(resource), class: "resource_id_link" 
      elsif controller.action_methods.include?('edit') 
       link_to resource.id, edit_resource_path(resource), class: "resource_id_link" 
      else 
       resource.id 
      end 
      end 
     end 


     def actions(options = {}, &block) 
      name   = options.delete(:name)  { '' } 
      defaults  = options.delete(:defaults) { true } 
      dropdown  = options.delete(:dropdown) { false } 
      dropdown_name = options.delete(:dropdown_name) { I18n.t 'active_admin.dropdown_actions.button_label', default: 'Actions' } 

      options[:class] ||= 'col-actions' 

      column name, options do |resource| 
      if dropdown 
       dropdown_menu dropdown_name do 
       defaults(resource) if defaults 
       instance_exec(resource, &block) if block_given? 
       end 
      else 
       table_actions do 
       defaults(resource, css_class: :member_link) if defaults 
       if block_given? 
        block_result = instance_exec(resource, &block) 
        text_node block_result unless block_result.is_a? Arbre::Element 
       end 
       end 
      end 
      end 
     end 

     private 

     def defaults(resource, options = {}) 
      if controller.action_methods.include?('show') && authorized?(ActiveAdmin::Auth::READ, resource) 
      item I18n.t('active_admin.view'), resource_path(resource), class: "view_link #{options[:css_class]}", title: I18n.t('active_admin.view') 
      end 
      if controller.action_methods.include?('edit') && authorized?(ActiveAdmin::Auth::UPDATE, resource) 
      item I18n.t('active_admin.edit'), edit_resource_path(resource), class: "edit_link #{options[:css_class]}", title: I18n.t('active_admin.edit') 
      end 
      if controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, resource) 
      item I18n.t('active_admin.delete'), resource_path(resource), class: "hippa_delete_link #{options[:css_class]}", title: I18n.t('active_admin.delete'), 
       method: :delete, data: {confirm: "Reason for deletion?", inputs: {comment: :textarea}} 
      end 
     end 

     class TableActions < ActiveAdmin::Component 
      builder_method :table_actions 

      def item *args 
      text_node link_to *args 
      end 
     end 
     end # IndexTableFor 

    end # IndexAsTable 
    end 
end 


## custom blank slate 
class HippaBlankSlate < ActiveAdmin::Component 
    builder_method :blank_slate 

    def default_class_name 
    'blank_slate_container' 
    end 

    def build(content) 
    super(span("You must first filter this resource to view.", class: "blank_slate")) 
    end 
end 


HIPPA_CLASS_ARRAY = %w(DailySummary Doctor Parent Patient PrescribedTension Prescription Product Sensor SensorEvent Treatment) 

## resource overrid 
class ActiveAdmin::Resource 
    module ActionItems 
    def initialize(*args) 
     super 
     add_default_action_items 
    end 

    def add_default_show_action_item 
     if HIPPA_CLASS_ARRAY.include? resource_class.name 
     add_action_item :destroy, only: [:show, :edit] do 
      if controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, resource) 
      localizer = ActiveAdmin::Localizers.resource(active_admin_config) 
      link_to localizer.t(:delete_model), resource_path(resource), class: "hippa_delete_link", method: :delete, 
       data: {confirm: "Reason for deletion?", inputs: {comment: :textarea}} 
      end 
     end 
     else 
     add_action_item :destroy, only: [:show, :edit] do 
      if controller.action_methods.include?('destroy') && authorized?(ActiveAdmin::Auth::DESTROY, resource) 
      localizer = ActiveAdmin::Localizers.resource(active_admin_config) 
      link_to localizer.t(:delete_model), resource_path(resource), method: :delete, 
       data: {confirm: localizer.t(:delete_confirmation)} 
      end 
     end 
     end 
    end 

    end 
end 











## resource controller overrides 

class ActiveAdmin::ResourceController 
    include InheritedResources::DSL 


    def apply_filtering(chain) 
     if params['q'].blank? && (HIPPA_CLASS_ARRAY.include? self.resource_class.name) 
     @search = chain.ransack({}) 
     chain.none 
     else 
     super 
     end 
    end 



    def setup_comments 
     klassname = self.resource_class.name.underscore 
     if params[klassname][:comments_attributes]['0']['body'].blank? 
     err = "A comment must be added to #{params[:action]} this #{klassname}." 
     else 
     params[klassname][:comments_attributes]['0']['namespace'] = 'admin' 
     params[klassname][:comments_attributes]['0']['author_id'] = current_admin_user.id 
     params[klassname][:comments_attributes]['0']['author_type'] = 'AdminUser' 
     end 
     if !err.nil? 
     params[:error] = err 
     end 
     return 
    end 


    def update(options={}, &block) 
     if HIPPA_CLASS_ARRAY.include? self.resource_class.name 
     setup_comments 
     # save resource 
     if params[:error].nil? 
      super 
      if resource.errors.any? 
      params[:error] = resource.errors.full_messages.first 
      end 
     end 
     # see if any error messages 
     if !params[:error].nil? 
      redirect_to({ action: 'edit' }, alert: params[:error]) 
     end 

     else 
     super 
     end 
    end 


    def create 
     if HIPPA_CLASS_ARRAY.include? self.resource_class.name 
     setup_comments 
     if params[:error].nil? 
      resource = self.resource_class.new(permitted_params[self.resource_class.name.underscore.to_sym]) 
      @comment=ActiveAdmin::Comment.new(permitted_params[self.resource_class.name.underscore.to_sym][:comments_attributes]['0']) 
      @comment.resource = resource 
      resource.comments.first.resource = resource 

      if resource.valid? 
      resource.save 
      else 
      if resource.errors.any? 
       params[:error] = resource.errors.full_messages.first 
       end 
      end 
     end 


     if !params[:error].nil? 
      redirect_to({ action: 'index' }, alert: params[:error]) 
     else 
      redirect_to({ action: 'index' }, alert: "#{resource_class} was successfully saved with comment.") 
     end 
     else 
     super 
     end 
    end 

    def destroy 
     if HIPPA_CLASS_ARRAY.include? self.resource_class.name 
     if !params[:comment].nil? && !params[:comment].empty? 
      @comment=ActiveAdmin::Comment.new(namespace: "admin", author: current_admin_user, resource: resource, body: params[:comment] ) 
      @comment.save 

      resource.destroy 

      redirect_to({ action: 'index' }, notice: "Delete was successful."); 
     else 
      flash[:notice] = "A delete comment can not be blank." 
      render :js => 'window.location.reload()' 
     end 
     else 
     super 
     end 
    end 
    end 

나는 심지어 '삭제'링크 나 버튼이 ActiveAdmin.dialog를 사용하여 클릭 할 때 주석에 입력 삭제/팝업 대화 상자를 할 수 있었다. 해시 :이 무엇

(function() { 
    $(document).on('ready page:load turbolinks:load', function() { 
    $('.hippa_delete_link').click(function(e) { 
     var message; 
     e.stopPropagation(); 
     e.preventDefault(); 
     if (message = $(this).data('confirm')) { 
     return ActiveAdmin.modal_dialog(message, $(this).data('inputs'), (function(_this) { 
      return function(inputs) { 
      return $(_this).trigger('confirm:complete', inputs); 
      }; 
     })(this)); 
     } else { 
     return $(this).trigger('confirm:complete'); 
     } 
    }); 
    $('.hippa_delete_link').on('confirm:complete', function(e, inputs) { 

     var url = this.href; 

     $.ajax({ 
      data: inputs, 
      url: url, 
      type: "DELETE" 
     }); 
    }); 
    }); 

}).call(this); 

가 { "텍스트 입력"코멘트}를 전달할 수 있습니다 : 당신이 위에서 보면 나는이 추가 자바 스크립트에 의해 선택 될 것 hippa_delete_link 클래스에 delete_link 클래스를 변경 삭제 방법.