Added dump to yml and load from yml abilities for roles.

This commit is contained in:
George Vinogradov 2011-10-26 04:33:25 +04:00
parent f15f2ce98d
commit 02a8a62f36
5 changed files with 139 additions and 22 deletions

View File

@ -51,7 +51,37 @@ class RolesController < ApplicationController
flash[:notice] = t("flash.role.destroyed")
redirect_to roles_path
end
def get_dump
file = Role.save_dump
send_file file, :type => 'text/plain'
end
def load_from_dump
flag = true
puts params.inspect
puts File.extname(params[:file].original_filename)
unless ['.yml', '.yaml'].include? File.extname(params[:file].original_filename)
flash[:error] = t("layout.roles.wrong_file_type")
flag = false
end
if flag
t = YAML.load params[:file].tempfile
unless t.is_a? Hash and t[:Roles]
flash[:error] = t("layout.roles.wrong_file_format")
flag = false
else
begin
Role.all_from_dump! t
flash[:notice] = t("layout.roles.successful_load")
rescue
flash[:error] = t("layout.roles.seeding_fail")
end
end
end
redirect_to :back
end
protected
def find_role
@role = Role.find(params[:id])

View File

@ -6,4 +6,67 @@ class Role < ActiveRecord::Base
serialize :can_see, Hash
validate :name, :presence => true
def to_dump
tmp = attributes.reject {|k,v| ['created_at', 'updated_at'].include? k}
tmp['rights'] = rights.inject({}) do |h, right|
h[right.controller] ||= []
h[right.controller] << right.action
h[right.controller].uniq!
h
end
return tmp
end
class << self
def save_dump filename = 'config/roles.yml'
fn = File.expand_path filename
File.open (fn, 'w'){|f| f.puts dump_roles}
return filename
end
def dump_roles
roles = Role.find(:all, :include => :rights)
roles = roles.map(&:to_dump)
return {:Roles => roles}.to_yaml
end
def all_from_dump! dump_hash
arr = []
dump_hash[:Roles].each do |role|
arr << from_dump!(role)
end
arr
end
def all_from_dump dump_hash
arr = []
dump_hash[:Roles].each do |role|
arr << from_dump(role)
end
arr
end
def from_dump! fields
from_dump(fields).save
end
def from_dump fields
rights = fields.delete('rights')
a = find(fields['id']) || new
a.rights = []
a.attributes = fields
rights.each do |con, acts|
acts.each do |act|
unless r = Right.where(:controller => con, :action => act)
r = Right.create(:controller => con, :action => act)
end
a.rights << r
end
end
return a
end
end
end

View File

@ -7,24 +7,32 @@
%h2.title
= t("layout.roles.list_header")
.inner
-unless @roles.empty?
%table.table
%tr
%th.first ID
%th= t("activerecord.attributes.role.name")
%th.last &nbsp;
- @roles.each do |role|
%tr{:class => cycle("odd", "even")}
%td
= role.id
%td
= link_to role.name, role_path(role)
%td.last
#{link_to t("layout.show"), role_path(role)} | #{link_to t("layout.edit"), edit_role_path(role)} | #{link_to t("layout.delete"), role_path(role), :method => :delete, :confirm => t("layout.roles.confirm_delete")}
.actions-bar.wat-cf
.actions
-else
.inner
%label.label Роли отсутствуют,
= link_to "создать новую роль", new_role_path
.group
= button_to t("layout.roles.get_dump"), {:controller => :roles, :action => :get_dump},
{:style => 'float: left; margin-right: 30px'}
= form_tag url_for(:controller => :roles, :action => :load_from_dump), :multipart => true do
= label_tag :file, t("layout.roles.from_file") + ':'
= file_field_tag :file
= submit_tag t("layout.upload")
%br
-unless @roles.empty?
%table.table
%tr
%th.first ID
%th= t("activerecord.attributes.role.name")
%th.last &nbsp;
- @roles.each do |role|
%tr{:class => cycle("odd", "even")}
%td
= role.id
%td
= link_to role.name, role_path(role)
%td.last
#{link_to t("layout.show"), role_path(role)} | #{link_to t("layout.edit"), edit_role_path(role)} | #{link_to t("layout.delete"), role_path(role), :method => :delete, :confirm => t("layout.roles.confirm_delete")}
.actions-bar.wat-cf
.actions
-else
.inner
%label.label Роли отсутствуют,
= link_to "создать новую роль", new_role_path
- content_for :sidebar, render(:partial => 'sidebar')

View File

@ -21,6 +21,7 @@ ru:
false_: Нет
publish: Опубликовать
add: Добавить
upload: Загрузить
downloads:
title: Статистика закачек пакетов
@ -95,6 +96,15 @@ ru:
roles:
confirm_delete: Вы уверены, что хотите удалить эту роль?
list_header: Роли
list: Список
new: Создать
get_dump: Скачать в YML
from_file: Загрузить из файлa
wrong_file_type: Неверный тип файла
wrong_file_format: Неверный формат файла
successful_load: Все роли успешно загружены
seeding_fail: Ошибка при записи в базу
event_logs:
list: Список

View File

@ -9,7 +9,13 @@ Rosa::Application.routes.draw do
resources :users
resources :roles
resources :roles do
collection do
get 'get_dump'
post 'get_dump'
post 'load_from_dump'
end
end
resources :event_logs, :only => :index