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
-
#belongs_to_for(from, to, options = {}) ⇒ Object
TODO: Needs extraction into helper module!.
- #boolean?(value) ⇒ Boolean
- #get_model_type(class_name) ⇒ Object
-
#has_and_belongs_many(from, to, options = {}) ⇒ Object
To setup sth like this:.
- #has_many_for(from, to, options = {}) ⇒ Object
- #has_one_for(from, to, options = {}) ⇒ Object
- #key_options(type, obj) ⇒ Object
- #make_key(name, options = {}) ⇒ Object
-
#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).
- #object_key(options = {}) ⇒ Object
- #valid_field_name?(name) ⇒ Boolean
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, = {} make_relationship :belongs_to, from, to, end |
#boolean?(value) ⇒ 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, = {} make_relationship :has_and_belongs_to_many, from, to, :key => role_field make_relationship :has_and_belongs_to_many, to, from, 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, = {} make_relationship :has_many, from, to, 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, = {} make_relationship :has_one, from, to, end |
#key_options(type, obj) ⇒ Object
77 78 79 80 |
# File 'lib/troles/common/config/schema/helpers.rb', line 77 def 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, = {} 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 [: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, = {} # puts "type: #{type}, #{from}, #{to}" from_type = get_model_type from to_type = get_model_type to key_opts = (type, to_type) model_key = [:key] ? [:key] : send("#{to_type}_key", key_opts) class_name = send "#{to_type}_class_name" = {:class_name => class_name} .merge!([:opts]) if [:opts] puts "#{from}.#{type} :#{model_key}, #{.inspect}" if log_on? from.send(type, model_key, ) end |
#object_key(options = {}) ⇒ Object
73 74 75 |
# File 'lib/troles/common/config/schema/helpers.rb', line 73 def object_key = {} make_key "#{object_class_name}", end |
#valid_field_name?(name) ⇒ Boolean
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 |