Skip to content

Commit 1ec8514

Browse files
committed
[STORE] Added a Rails ORM generator for Elasticsearch::Persistence::Model
Usage: $ bundle exec rails generate scaffold Person name:String email:String --orm=elasticsearch --force
1 parent e3a4414 commit 1ec8514

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Diff for: elasticsearch-persistence/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ article.published.iso8601
600600
# => "2014-01-01"
601601
```
602602

603+
The library provides a Rails ORM generator:
604+
605+
```bash
606+
rails generate scaffold Person name:String email:String birthday:Date --orm=elasticsearch
607+
```
608+
603609
## License
604610

605611
This software is licensed under the Apache 2 license, quoted below.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "rails/generators/elasticsearch_generator"
2+
3+
module Elasticsearch
4+
module Generators
5+
class ModelGenerator < ::Rails::Generators::NamedBase
6+
source_root File.expand_path('../templates', __FILE__)
7+
8+
desc "Creates an Elasticsearch::Persistence model"
9+
argument :attributes, type: :array, default: [], banner: "attribute:type attribute:type"
10+
11+
check_class_collision
12+
13+
def create_model_file
14+
@padding = attributes.map { |a| a.name.size }.max
15+
template "model.rb.tt", File.join("app/models", class_path, "#{file_name}.rb")
16+
end
17+
18+
hook_for :test_framework
19+
end
20+
end
21+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% module_namespacing do -%>
2+
class <%= class_name %>
3+
include Elasticsearch::Persistence::Model
4+
5+
<% attributes.each do |attribute| -%>
6+
<%= "attribute :#{attribute.name},".ljust(@padding+12) %> <%= attribute.type %>
7+
<% end -%>
8+
end
9+
<% end -%>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "rails/generators/named_base"
2+
require "rails/generators/active_model"

0 commit comments

Comments
 (0)