Module: Troles::Common::Config::Schema::Helpers

Included in:
Troles::Common::Config::Schema
Defined in:
lib/troles/common/config/schema/helpers.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_for(from, to, options = {}) ⇒ Object

TODO: Needs extraction into helper module!



17
18
19
# File 'lib/troles/common/config/schema/helpers.rb', line 17

def belongs_to_for from, to, options = {}
  make_relationship :belongs_to, from, to, options
end

#boolean?(value) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/troles/common/config/schema/helpers.rb', line 11

def boolean? value
  [true, false].include? value
end

#get_model_type(class_name) ⇒ Object



43
44
45
46
47
48
# File 'lib/troles/common/config/schema/helpers.rb', line 43

def get_model_type class_name
  return :subject if class_name == subject_class
  return :object  if class_name == object_model
  return :join    if class_name == role_join_model
  raise "Not a known model: #{class_name}"
end

#has_and_belongs_many(from, to, options = {}) ⇒ Object

To setup sth like this:

class UserAccount < ActiveRecord::Base

has_and_belongs_to_many :troles, :class_name => 'Role'

end

class Role < ActiveRecord::Base

has_and_belongs_to_many :user_accounts, :class_name => 'User'

end



38
39
40
41
# File 'lib/troles/common/config/schema/helpers.rb', line 38

def has_and_belongs_many from, to, options = {}
  make_relationship :has_and_belongs_to_many, from, to, :key => role_field
  make_relationship :has_and_belongs_to_many, to, from, options
end

#has_many_for(from, to, options = {}) ⇒ Object



21
22
23
# File 'lib/troles/common/config/schema/helpers.rb', line 21

def has_many_for from, to, options = {}
  make_relationship :has_many, from, to, options
end

#has_one_for(from, to, options = {}) ⇒ Object



25
26
27
# File 'lib/troles/common/config/schema/helpers.rb', line 25

def has_one_for from, to, options = {}
  make_relationship :has_one, from, to, options
end

#key_options(type, obj) ⇒ Object



77
78
79
80
# File 'lib/troles/common/config/schema/helpers.rb', line 77

def key_options type, obj
  return {:prefix => 't'} if (type == :has_many && obj == :object) 
  {}
end

#make_key(name, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/troles/common/config/schema/helpers.rb', line 82

def make_key name, options = {}
  name = name.to_s.gsub(/::/, '__').underscore.pluralize
  parts = name.split('_')
  name = parts.inject([]) do |res, part|
    res << (part != parts.last ? part.singularize : part)
    res
  end.join('_')
  
  key = name
  options[:prefix] ? "t#{key}" : key
end

#make_relationship(type, from, to, options = {}) ⇒ Object

options:

  • :opts, extras options, fx to set the :through relationship

  • :key (usually to enforce use of role_field as key name)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/troles/common/config/schema/helpers.rb', line 53

def make_relationship type, from, to, options = {}
  # puts "type: #{type}, #{from}, #{to}"
  from_type = get_model_type from
  to_type = get_model_type to

  key_opts = key_options(type, to_type)

  model_key = options[:key] ? options[:key] : send("#{to_type}_key", key_opts)

  class_name = send "#{to_type}_class_name"

  rel_options = {:class_name => class_name}   

  rel_options.merge!(options[:opts]) if options[:opts]

  puts "#{from}.#{type} :#{model_key}, #{rel_options.inspect}" if log_on?
  
  from.send(type, model_key, rel_options)
end

#object_key(options = {}) ⇒ Object



73
74
75
# File 'lib/troles/common/config/schema/helpers.rb', line 73

def object_key options = {}
  make_key "#{object_class_name}", options
end

#valid_field_name?(name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentException)


5
6
7
8
9
# File 'lib/troles/common/config/schema/helpers.rb', line 5

def valid_field_name? name
  return false if !name || name.empty?
  raise ArgumentException, "Role field must not be named role or roles as these names are reserved by troles!" if [:role, :roles].include? name.to_sym
  true
end