2

データベース構造に基づいて GUI を構築するためのアクティブな足場を備えた ruby​​-on-rails アプリがあります。ユーザーにはロールがあり、各ロールは一連の権限です。各権限は、ユーザーがこのコントローラーで実行することを許可されているかどうかに関係なく、コントローラーとアクションの組み合わせです。

# DATABASE!
create_table :rights do |t|
  t.column :controller, :string, :limit => 32, :null => false
  t.column :action, :string, :limit => 32, :null => false
end

create_table :rights_roles, :id => false do |t|
  t.column :right_id,      :integer
  t.column :role_id,       :integer
end

create_table :roles do |t|
  t.column :name, :string, :limit => 32, :null => false
end

#MODELS!
class Role < ActiveRecord::Base
  has_and_belongs_to_many :rights

class Right < ActiveRecord::Base
  has_and_belongs_to_many :roles

# ROLE CONTROLLER!
class RoleController < ApplicationController
  active_scaffold :role do |config|
    config.columns = Array[:name, :rights]    
    config.columns[:rights].form_ui = :select

私は現在、不便なロールの次の編集ウィンドウを持っています (オプションは構造化されていません。より多くのアクションがあるため、恐ろしいでしょう):

http://postimage.org/image/4e8ukk2px/

次のようなヘルパー メソッドを作成します。

module RoleHelper
  def rights_form_column (record, input_name) 
    ...
  end
end

これは、「権限」列の入力方法を指定するフォームを定義するために必要です。しかし、私はそれを書く方法がわかりません。望ましい形式は次のとおりです。

  administration
    action1(checkbox)
    action2(checkbox)
  configuration
    action1(checkbox)
    ...

activescaffold が古いことはわかっていますが、使用する必要があります... 助けてください!

4

2 に答える 2

0

ついに私は解決策を見つけました。これは私にとってはうまくいきます=)

require 'set'

module RoleHelper
  def rights_form_column (record, input_name)
    selected_ids = [].to_set
    record.rights.each do |r|
      selected_ids.add(r.id)
    end

html = '<table style="margin-top:-25; margin-left:112">'
html << '<tr>'
html << '<td>'
html << '<ul style="list-style-type:none">'
Right.find(:all, :group => :controller).each do |right|
  unless Right::CONFIGURATION_CONTROLLERS.include?(right.controller) then
    html << '<li>'
    html << "<label>#{right.controller.titleize}:</label>"

    html << '<ul style="list-style-type:none">'
    Right.find(:all, :conditions => ["controller = ?", right.controller]).each do |r|
      html << '<li>'
      this_name = "#{input_name}[#{r.id}][id]"
      html << check_box_tag(this_name, r.id, selected_ids.include?(r.id))
      html << "<label for='#{this_name}'>"
      html << r.action.titleize
      html << '</label>'
      html << '</li>'
    end      
    html << '</ul>'
    html << '</li>'
  end
end

html << '<li>' # configuration section
html << "<label>Configuration:</label>"
html << '<ul style="list-style-type:none">'

Right.find(:all, :group => :controller).each do |right|
  if Right::CONFIGURATION_CONTROLLERS.include?(right.controller) then
    Right.find(:all, :conditions => ["controller = ?", right.controller]).each do |r|
      html << '<li>'
      this_name = "#{input_name}[#{r.id}][id]"
      html << check_box_tag(this_name, r.id, selected_ids.include?(r.id))
      html << "<label for='#{this_name}'>"
      html << r.controller.titleize + " edit"


  html << '</label>'
          html << '</li>'
        end      
      end
    end

html << '</ul>'
html << '</li>'
html << '</ul>'
html << '</td>'
html << '</tr>'
html << '</table>'
html
  end
end

StackOverflowがそのような奇妙なコードフォーマットを選んだ理由

于 2012-09-05T16:21:55.267 に答える