class Locomotive::Wagon::Generators::Relationship

Public Instance Methods

content_types_must_exist() click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 19
def content_types_must_exist
  unless File.exists?(File.join(destination_root, source_path))
    fail Thor::Error, "The #{source} content type does not exist"
  end

  unless File.exists?(File.join(destination_root, target_path))
    fail Thor::Error, "The #{target} content type does not exist"
  end
end
modify_content_types() click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 29
def modify_content_types
  case type.to_sym
  when :belongs_to
    append_to_file source_path, build_belongs_to_field(source, target)
    append_to_file target_path, build_has_many_field(target, source)
  when :has_many
    append_to_file source_path, build_has_many_field(source, target)
    append_to_file target_path, build_belongs_to_field(target, source)
  when :many_to_many
    append_to_file source_path, build_many_to_many_field(source, target)
    append_to_file target_path, build_many_to_many_field(target, source)
  else
    fail Thor::Error, "#{type} is an unknown relationship type"
  end
end

Protected Instance Methods

build_belongs_to_field(source_class, target_class) click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 55
def build_belongs_to_field(source_class, target_class)
  in_yaml({
    target_class.singularize => {
      'label'       => target_class.singularize.humanize,
      'hint'        => 'A description of the relationship for the editors',
      'type'        => 'belongs_to',
      'class_name'  => target_class
    }
  })
end
build_has_many_field(source_class, target_class) click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 66
def build_has_many_field(source_class, target_class)
  in_yaml({
    target_class => {
      'label'       => target_class.humanize,
      'hint'        => 'A description of the relationship for the editors',
      'type'        => 'has_many',
      'class_name'  => target_class,
      'inverse_of'  => source_class.singularize,
      'ui_enabled'  => true
    }
  })
end
build_many_to_many_field(source_class, target_class) click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 79
def build_many_to_many_field(source_class, target_class)
  in_yaml({
    target_class => {
      'label'       => target_class.humanize,
      'hint'        => 'A description of the relationship for the editors',
      'type'        => 'many_to_many',
      'class_name'  => target_class,
      'inverse_of'  => source_class,
      'ui_enabled'  => true
    }
  })
end
in_yaml(hash) click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 92
def in_yaml(hash)
  [hash].to_yaml.gsub(/^(---\s+)/, "\n")
end
source_path() click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 47
def source_path
  "app/content_types/#{source}.yml"
end
target_path() click to toggle source
# File lib/locomotive/wagon/generators/relationship.rb, line 51
def target_path
  "app/content_types/#{target}.yml"
end