所以我有一些Rails模型。这是一个类似的示例列表:
我想得到一个非常神奇的功能,我可以使用如下:
(resource, scope, scope_ids) => {...}
成功的通话如下:
("house", "country", [30]) => { neighborhood: { city: { state: { country: [30] } } } }
("house", "state", [1,2,3]) => { neighborhood: { city: { state: [1,2,3] } } }
并且失败的电话会返回:
("house", "people", [1,2,3]) => { id: -1 }
我想自动神奇地这样做,所以任何硬编码都是不可接受的。
编辑:
我想实现上述结果,因为我正在使用CanCanCan开发一个允许执行以下操作的授权层:
can :read, House, { neighborhood: { city: { state: { name: "Illinois" }}}}
考虑到上面的语法,我想自动神奇地生成权限,例如(例如“own_neightborhood”是被授权用户的neightborhood,“others_houses”是用户的房屋,与被授权的用户不同):
# permission read_own_neighborhood_houses should generate:
can :read, House, { neighborhood: { users: {id: user.id } }
# permission read_own_state_cities should generate:
can :read, City, { state: { cities: { houses: { users: {id: user.id } } } } } } }
# permission read_others_neighborhoods_states should generate:
can :read, State, { cities: { neighborhoods: { houses: { users: { id: User.where.not(id: user.id) } } } } }
答案 0 :(得分:1)
这当然需要相当多的元编程。这可能是使用public showPlan:boolean = false; //For showPlan
ngOnInit() {
var arg1 = this.showPlan;
var arg2 = this.showAccount;
console.log('arg1--------'+arg1); //coming fine on initial
jQuery(document).click({showPlan:arg1},function(e) {
console.log('showPlan--------'+e.data.showPlan); //coming as false insted true
if(e.data.showPlan != 'false') { //this should trigger (not triggering because it is not updating to true)
jQuery("#planTabInput").css( "display",'none' );
}
});
}
public displayPlanWidget() {
this.showPlan = true;
console.log('showPlan Changed to '+this.showPlan); //coming true(fine)
jQuery("#planTabInput").show();
}
方法开始...
给定reflections_on_all_associations
permission read_own_state_cities
在没有实际情况的情况下,我可以轻松地进行测试。您需要根据def permission(perms)
@perms = perms.split('_') #['read', 'own', 'state', 'cities']
@model = @perms[-1].singularize.capitalize.constantize #City
@action = @perms[0].to_sym #:read
@ownership = @perms[1].to_sym #:own
@top_level = @perms[2] #:state
hash_items = []
current_level = @top_level
until current_level = :user do
hash_items << current_level
current_level = current_level.reflect_on_all_associations(:has_many).first.name.to_sym
end #[:state, :cities, :neighborhoods]
@hash = hash_items.reverse.inject({users: {}}) { |a, n| { n => a } } #{state: {cities: {neigborhoods: {users: {}}}}}
end
的值来确定如何决定users: {}
哈希的内容。这应该可以为您提供最终构建@ownership
动态所需的一切。