Module: Permissify::ModelClass

Included in:
Product, Role
Defined in:
lib/permissify/model_class.rb

Instance Method Summary collapse

Instance Method Details

#create_seeds(seed_specifications) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/permissify/model_class.rb', line 4

def create_seeds(seed_specifications)
  seed_specifications.each do |id, name|
    permissions_model = locate(id, name)
    permissions_model.permissions = send("#{underscored_name_symbol(name)}_permissions") # interface methods needed for each seeded model
    permissions_model.save
  end
end

#create_with_id(table, id, name) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/permissify/model_class.rb', line 12

def create_with_id(table, id, name)
  permissions = send("#{underscored_name_symbol(name)}_permissions")
  permissions_model = new
  permissions_model.name = name
  permissions_model.permissions = permissions
  permissions_model.save
  force_seed_id(table, permissions_model, id) # interface method : force_seed_id
  find(id)
end

#force_seed_id(table, permissions_model, id) ⇒ Object



22
23
24
25
# File 'lib/permissify/model_class.rb', line 22

def force_seed_id(table, permissions_model, id)
  # not sure if this is still needed, may differ depending on db adapter (written against mysql)
  ActiveRecord::Base.connection.execute "UPDATE #{table}s SET id=#{id} WHERE id=#{permissions_model.id};"
end

#locate(id, name) ⇒ Object



27
28
29
30
# File 'lib/permissify/model_class.rb', line 27

def locate(id, name)
  model_with_id   = find_by_id(id) # interface method : ActiveRecord::Base.find_by_id
  model_with_id ||= send("create_#{underscored_name_symbol(name)}") # interface methods needed for each seeded model
end

#underscored_name_symbol(name) ⇒ Object



32
33
34
# File 'lib/permissify/model_class.rb', line 32

def underscored_name_symbol(name)
  name.to_s.downcase.gsub(':','').gsub('-','_').gsub(' ','_').gsub(/__*/,'_')
end