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

@ -52,6 +52,36 @@ class RolesController < ApplicationController
redirect_to roles_path redirect_to roles_path
end 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 protected
def find_role def find_role
@role = Role.find(params[:id]) @role = Role.find(params[:id])

View File

@ -6,4 +6,67 @@ class Role < ActiveRecord::Base
serialize :can_see, Hash serialize :can_see, Hash
validate :name, :presence => true 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 end

View File

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

View File

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

View File

@ -9,7 +9,13 @@ Rosa::Application.routes.draw do
resources :users 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 resources :event_logs, :only => :index