cancan 里的 load_and_authorize_resourceend

使用

class BooksController < ApplicationController
  load_and_authorize_resourceend
end

对应

14
15
16
# File 'lib/cancan/controller_additions.rb', line 14

def load_and_authorize_resource(*args)
  cancan_resource_class.add_before_filter(self, :load_and_authorize_resource, *args)
end

这是对应

# File 'lib/cancan/controller_resource.rb', line 24
def load_and_authorize_resource
  load_resource
  authorize_resource
end

分为两部分:

一,

# File 'lib/cancan/controller_resource.rb', line 29

def load_resource
  unless skip?(:load)
    if load_instance?
      self.resource_instance ||= load_resource_instance
    elsif load_collection?
      self.collection_instance ||= load_collection
    end
  end
end

这又再次为分两部分:

# File 'lib/cancan/inherited_resource.rb', line 4

def load_resource_instance
  if parent?
    @controller.send :association_chain
    @controller.instance_variable_get("@#{instance_name}")
  elsif new_actions.include? @params[:action].to_sym
    resource = @controller.send :build_resource
    assign_attributes(resource)
  else
    @controller.send :resource
  end
end

def load_collection                                                 

  resource_base.accessible_by(current_ability, authorization_action)

end        


def current_ability

  @controller.send(:current_ability)

end


def authorization_action

  parent? ? :show : @params[:action].to_sym

end

---------

@articles = Article.accessible_by(current_ability)
@articles = Article.accessible_by(current_ability, :update)

                                                         

另一部分:

# File 'lib/cancan/controller_resource.rb', line 39

def authorize_resource
  unless skip?(:authorize)
    @controller.authorize!(authorization_action, resource_instance || resource_class_with_parent)
  end
end





转载于:https://my.oschina.net/kelby/blog/206655

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gspread是一个用于与Google表格进行交互的Python库,而append_row()是其中一种向Google表格中添加新行的方法。 使用append_row()方法时,需要先通过gspread库打开一个Google表格,并选择一个具体的工作表。然后,可以调用工作表对象的append_row()方法来向该工作表中添加新行。 append_row()方法的基本语法如下: ``` worksheet.append_row(row, value_input_option='USER_ENTERED') ``` 其中,worksheet表示要向其中添加新行的工作表对象,row是一个列表或元组,表示要添加到表格中的新行的数据。value_input_option参数是可选的,可以指定将值如何输入到新行中。默认情况下,值将使用用户输入的方式输入(即'USER_ENTERED')。 下面是一个使用append_row()方法向Google表格中添加新行的简单示例代码: ```python import gspread from oauth2client.service_account import ServiceAccountCredentials # 连接Google表格 scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope) client = gspread.authorize(creds) sheet = client.open('My Spreadsheet').sheet1 # 添加新行 new_row = ['John', 'Doe', '30'] sheet.append_row(new_row) ``` 在这个示例中,我们首先使用ServiceAccountCredentials.from_json_keyfile_name()方法连接到Google表格。然后,我们通过client.open()方法打开名为'My Spreadsheet'的Google表格,并选择第一个工作表(即sheet1)。最后,我们创建一个新的列表new_row,包含要添加到表格中的新行的数据,然后调用sheet.append_row()方法将其添加到表格的末尾。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值