Module: AuthAssist::MigrationHelper::CodeRefactor

Included in:
AuthAssist::MigrationHelper
Defined in:
lib/generators/auth_code_refactor.rb

Instance Method Summary collapse

Instance Method Details

#clear_user_relationsObject

erase



6
7
8
9
10
# File 'lib/generators/auth_code_refactor.rb', line 6

def clear_user_relations
  erase_in_user(has_roles_through_assignments)
  erase_in_user(has_roles)  
  erase_in_user(has_role_assignments)
end

#erase_in_user(txt) ⇒ Object



12
13
14
15
16
# File 'lib/generators/auth_code_refactor.rb', line 12

def erase_in_user(txt)  
  file = File.new(model_file('user'))
  return if !(file.read =~ /#{txt}/)         
  gsub_file model_file('user'), /#{Regexp.escape(txt + "\n")}/, ''
end

#has_role_assignmentsObject

refactor code



40
41
42
# File 'lib/generators/auth_code_refactor.rb', line 40

def has_role_assignments 
  'has_many :role_assignments'
end

#has_rolesObject



48
49
50
# File 'lib/generators/auth_code_refactor.rb', line 48

def has_roles 
  'has_many :roles'
end

#has_roles_through_assignmentsObject



44
45
46
# File 'lib/generators/auth_code_refactor.rb', line 44

def has_roles_through_assignments 
  'has_many :roles, :through => :role_assignments'
end

#insert_user_relation(relation) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/generators/auth_code_refactor.rb', line 26

def insert_user_relation(relation)
  file = File.new(model_file('user'))
  return if (file.read =~ /#{relation}/) 
  gsub_file model_file('user'), /class User < ActiveRecord::Base/ do |match|
    match << "\n  #{relation}"
  end
end

#remove_user_relation(relation) ⇒ Object



34
35
36
# File 'lib/generators/auth_code_refactor.rb', line 34

def remove_user_relation(relation)
  erase_in_user(relation)  
end

#role_assignment_file_contentObject



61
62
63
64
65
66
67
68
# File 'lib/generators/auth_code_refactor.rb', line 61

def role_assignment_file_content
  %q{
  class RoleAssignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
  end          
  }
end

#role_file_contentObject



52
53
54
55
56
57
58
59
# File 'lib/generators/auth_code_refactor.rb', line 52

def role_file_content
  %q{
  class Role < ActiveRecord::Base
  has_many :role_assignments
  has_many :users, :through => :role_assignments
  end          
  }    
end

#write_model_file(name, content) ⇒ Object

insert



20
21
22
23
24
# File 'lib/generators/auth_code_refactor.rb', line 20

def write_model_file(name, content)
  File.open(model_file(name), 'w+') do |f| 
    f.write(content) 
  end
end