`
myrev
  • 浏览: 163324 次
  • 性别: Icon_minigender_1
  • 来自: 福建
社区版块
存档分类
最新评论

Active Scaffold Note -- Rails Plugin

阅读更多
1. Install
./script/plugin install git://github.com/activescaffold/active_scaffold.git -r rails-2.3


Rails 2.3.x needs the plugin render_component for nested scaffolds
./script/plugin install git://github.com/ewildgoose/render_component.git -r rails-2.3


Getting Started :   http://wiki.github.com/activescaffold/active_scaffold/getting-started


2. Configuration

  1) Model

Security


On your model object you may define methods (none of which accept any arguments) in any of four formats, depending on your need for granularity.

The formats are:

    * #{column_name}_authorized_for_#{crud_type}?
    * #{column_name}_authorized?
    * authorized_for_#{crud_type}?
    * authorized_for_#{action_name}?

http://wiki.github.com/activescaffold/active_scaffold/security

    eg: Company

 
def authorized_for_delete?
    current_user.is_admin? || Role.is_company_admin(current_user)
  end
 

  def authorized_for_update?
    current_user.is_admin? || Role.is_company_admin(current_user)
  end
 

  def authorized_for_create?
    current_user.is_admin? || Role.is_company_admin(current_user)
  end


Task

 
def original_hours_authorized_for_update?
    current_user.is_admin? || Role.is_company_admin(current_user)
  end


named_scope

When you use beginning_of_chain method on controller, you can use model named_scope to limit the collection.



Tip: ”:joins” (ActiveRecord readonly), ”:include”


  2) Controller

DataGrid

config.columns = [...]

list.columns.exclude :attribute1, :attribute2

update.columns.exclude :attribute1, :attribute2

create.columns.exclude ::attribute1, :attribute2

show.columns.exclude ::attribute1, :attribute2


eg: stories_controller.rb

active_scaffold :story do |config|
    config.columns = [:title, :description, :idealdays, :priority, :state, :iteration, :tasks, :user, :comments, :project, :labels]
    list.columns.exclude :comments, :tasks
    update.columns.exclude :comments, :tasks, :project, :labels
    create.columns.exclude :comments, :tasks, :project
    show.columns.exclude :comments, :tasks


    columns[:user].label = "Assign to User"
    columns[:idealdays].label = "Ideal Days"


    config.columns[:user].actions_for_association_links = [:show]
    config.columns[:iteration].actions_for_association_links = [:show]
    config.columns[:project].actions_for_association_links = [:show]


    config.nested.add_link("Tasks", [:tasks])
    config.nested.add_link("Comments", [:comments])


    config.columns[:state].form_ui = :select
    config.columns[:state].options = {:options => ["Please estimate ideal days"].map(&:to_sym)}
    config.columns[:user].form_ui = :select
    config.columns[:iteration].form_ui = :select
    config.columns[:description].form_ui = :text_editor
    config.columns[:labels].form_ui = :select
    config.columns[:priority].form_ui = :select
    config.columns[:priority].options = {:include_blank => "none", :options => 1..5}

    config.subform.layout = :vertical
    config.list.empty_field_text = "none"
  end



Form

config.columns[:field_name].form_ui  # value :boolean, :calendar)date_select, :checkbox, :country, :select....



http://wiki.github.com/activescaffold/active_scaffold/api-column






Labels

config.columns[:field_name].label = "Some text"

Collection Condition

conditions_for_collection, beginning_of_chain(use named_scope)



http://wiki.github.com/activescaffold/active_scaffold/api-list


eg: stories_controller.rb

  
def beginning_of_chain
    if !is_admin
      super.for_common_user(current_user.projects)
    elsif Role.is_company_admin(current_user)
      super.for_company_admin(current_user.company)
    else
      super
    end
  end



#{before,after}_#{create,update}_save

eg: stories_controller.rb

 
  def before_create_save(record)
    record.project = @parent unless @parent.nil?
  end



Security


#{action_name}_authorized?

http://wiki.github.com/activescaffold/active_scaffold/security

eg: users_controller.rb

def add_existing_authorized?
    false
  end




  3) Helper

Form Association Options(exclude: edit)

When you create a task_session, you want to limit the users with  your company.

options_for_association_conditions(association)

eg: users_helper.rb

def options_for_association_conditions(association)
    if association.name == :users
      ['company_id = ?', current_user.company.id] unless current_user.is_admin?
    elsif association.name == :roles
      ['roles.id != ?', Role.find_by_name(Role::ROLE_ADMINISTRATOR).id] unless current_user.is_admin?
    else
      super
    end
  end



http://wiki.github.com/activescaffold/active_scaffold/custom-association-options



Field Overrides

#{column_name}_column(record)

eg: stories_helper.rb

 
def labels_column(record)
    temp = record.labels.inject([]){|result, label| label.user == current_user ? result << h(label.name) : result }
    if temp.any?
      temp.join(",")
    else
      # This way there's something to turn into a link if there are no roles associated with this record yet.
      active_scaffold_config.list.empty_field_text
    end
  end



http://wiki.github.com/activescaffold/active_scaffold/field-overrides


Form Overrides


  http://wiki.github.com/activescaffold/active_scaffold/form-overrides

  4) View

Template Overrides

Rendering your template overrides will wrap in this order:

   1. Wrapping Template
   2. ActiveScaffold Global Template Overrideapp/views/active_scaffold_overrides/template.html.erb
   3. ActiveScaffold Frontendvendor/plugins/active_scaffold/frontends/default/views/template.html.erb




http://wiki.github.com/activescaffold/active_scaffold/template-overrides



3. Tips

ID (How to get id ? Nested page)


session_index = "as:#{params[:eid]}"
session[session_index][:constraints].values.to_s.to_i



Frequency Template Override



_list_actions.html : DataGrid Actions
_list_header.html : DataGrid Header Actions(Create, Search)
_list_column_headings.html : DataGrid Sort Action
_add_existing_form.html : many-to-many, add link


config.nested.shallow_delete : delete many-to-many link


true or false(default)


config.list.empty_field_text(default:”-”) : display text if field is nil

you can use "none" or other text.


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics