Sha256: 6faff21a9e88f51dc14af0133122363a69dfd0b81605d3e7737b5f1a79dbcbaa
Contents?: true
Size: 1.76 KB
Versions: 2
Compression:
Stored size: 1.76 KB
Contents
module MDWA module Generators class ModelAssociation attr_accessor :model1, :model2, :relation ACCEPTED_RELATIONS = [:has_many, :belongs_to, :has_and_belongs_to_many, :nested_many, :nested_one, :has_one] def initialize(model1_name, model2_name, relation_name) self.model1 = model1_name self.model2 = model2_name self.relation = relation_name # validation raise "Invalid model name: #{@model1.name}" unless self.model1.valid? raise "Invalid model name: #{@model2.name}" unless self.model2.valid? raise "Invalid relation type: #{@relation}" unless relation_valid? end def model1=(value) if value.is_a? String @model1 = Generators::Model.new( value ) elsif value.is_a? Generators::Model @model1 = value end end def model2=(value) if value.is_a? String @model2 = Generators::Model.new( value ) elsif value.is_a? Generators::Model @model2 = value end end def relation_valid? ACCEPTED_RELATIONS.include? relation.to_sym end def ordered [@model1, @model2].sort! {|a,b| a.plural_name <=> b.plural_name} end def belongs_to? return (relation == 'belongs_to') end def has_many? return (relation == 'has_many') end def nested_many? return (relation == 'nested_many') end def nested? nested_many? || nested_one? end def nested_one? return (relation == 'nested_one') end def has_one? return (relation == 'has_one') end def has_and_belongs_to_many? return (relation == 'has_and_belongs_to_many') end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mdd-3.0.2 | lib/mdwa/generators/model_association.rb |
mdd-3.0.1 | lib/mdwa/generators/model_association.rb |