Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: kucaahbe/annotated-rails
base: 6e4b9aaf6d
...
head fork: kucaahbe/annotated-rails
compare: a2679866d8
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
30 lib/annotated/models.rb
@@ -51,11 +51,11 @@ def annotation
# #{ANNOTATION % 'above'}
#++
annfooter
- annotation_body = []
+ annotation_table = []
# append table name:
- annotation_body << "# Table name: #{table_name}"
- annotation_body << '#'
+ annotation_table << "# Table name: #{table_name}"
+ annotation_table << '#'
# TODO check if abstract class (http://apidock.com/rails/ActiveRecord/Base/inspect/class)
column_with_maximum_name_length = @model.columns.map(&:name).max_by { |c| c.length }.length
@@ -70,10 +70,9 @@ def annotation
other_column_attrs << NOT_NULL unless column.null
other_column_attrs << DEFAULT % column.default if column.default
# https://github.com/rails/rails/blob/965f6f22aaee3b669d185b2ea2e125abaf458a63/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L227
- # TODO indexes
- # TODO SQL type at the end of table?
+ # todo SQL type at the end of table?
- annotation_body << sprintf(
+ annotation_table << sprintf(
%Q|# %s %-#{column_with_maximum_name_length+1}s:%-16s%s|,
column.primary && PRIMARY_KEY_MARK || ' ',
column.name,
@@ -82,7 +81,24 @@ def annotation
).rstrip
end
- annotation_header + annotation_body.join("\n") + annotation_footer
+ # annotating indexes if present
+ annotation_index = []
+ if (indexes = ActiveRecord::Base.connection.indexes(table_name)) and not indexes.empty?
+ annotation_index << "\n#"
+ annotation_index << "# Indexes:"
+ maximum_index_name_length = indexes.map(&:name).max_by { |c| c.length }.length
+ indexes.each do |index|
+ # todo add other index fields
+ annotation_index << sprintf(
+ "# %-#{maximum_index_name_length}s %s %s",
+ index.name,
+ index.columns.join(','),
+ index.unique ? 'unique' : ''
+ ).rstrip
+ end
+ end
+
+ annotation_header + annotation_table.join("\n") + annotation_index.join("\n") + annotation_footer
end
def add_regexp
View
3  spec/annotated/models/annotator_spec.rb
@@ -32,6 +32,9 @@
# decimal_column_with_scale :decimal
# created_at :datetime not null
# updated_at :datetime not null
+#
+# Indexes:
+# index_posts_on_author_name author_name
#--
# generated by 'annotated-rails' gem, please do not remove this line and content above
#++
View
2  spec/dummy/db/schema.rb
@@ -29,4 +29,6 @@
t.datetime "updated_at", :null => false
end
+ add_index "posts", :author_name
+
end

No commit comments for this range

Something went wrong with that request. Please try again.