class Sailsify::Generators::Model

Public Instance Methods

columns() click to toggle source
# File lib/sailsify/generators/model.rb, line 8
def columns
  @columns ||= model.columns.select { |c| foreign_key_columns.exclude?(c.name) }
end
render_associations() click to toggle source
# File lib/sailsify/generators/model.rb, line 13
def render_associations
  associations.map { |a| render_association(a) }.join(',')
end
table_name() click to toggle source
# File lib/sailsify/generators/model.rb, line 4
def table_name
  model.table_name
end

Private Instance Methods

associations() click to toggle source
# File lib/sailsify/generators/model.rb, line 23
def associations
  @associations ||= model.reflect_on_all_associations
end
custom_locals() click to toggle source
# File lib/sailsify/generators/model.rb, line 19
def custom_locals
  { :@model => self }
end
file_name() click to toggle source
# File lib/sailsify/generators/model.rb, line 62
def file_name
  "#{model.name}.js"
end
foreign_key_columns() click to toggle source
# File lib/sailsify/generators/model.rb, line 58
def foreign_key_columns
  @foreign_key_columns ||= associations.select { |a| a.macro == :belongs_to }.map(&:foreign_key).uniq
end
render_association(association) click to toggle source
# File lib/sailsify/generators/model.rb, line 31
def render_association(association)
  case association.macro
  when :belongs_to, :has_one
    render_bt_or_ho(association)
  when :has_many
    render_has_many(association)
  end
end
render_bt_or_ho(association) click to toggle source
# File lib/sailsify/generators/model.rb, line 40
      def render_bt_or_ho(association)
        <<-eos
        #{association.name.to_s.camelize(:lower)}: {
            model: '#{association.class_name}',
            columnName: '#{association.foreign_key}'
          }
        eos
      end
render_has_many(association) click to toggle source
# File lib/sailsify/generators/model.rb, line 49
      def render_has_many(association)
        <<-eos
        #{association.name.to_s.camelize(:lower)}: {
            collection: '#{association.class_name}',
            via: '#{association.active_record.class_name.camelize(:lower)}'
          }
        eos
      end
sub_dir() click to toggle source
# File lib/sailsify/generators/model.rb, line 66
def sub_dir
  'models'
end
supported_associations() click to toggle source
# File lib/sailsify/generators/model.rb, line 27
def supported_associations
  %i(has_one belongs_to has_many)
end
template_path() click to toggle source
# File lib/sailsify/generators/model.rb, line 70
def template_path
  File.expand_path('../../templates/model.js.erb', __FILE__)
end