rubyblok 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +0 -139
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +120 -92
  5. data/Rakefile +1 -1
  6. data/lib/generators/rubyblok/hello_world_generator.rb +45 -0
  7. data/lib/generators/rubyblok/image_cache_generator.rb +77 -0
  8. data/lib/generators/rubyblok/install_generator.rb +7 -5
  9. data/lib/generators/rubyblok/migration_generator.rb +10 -8
  10. data/lib/generators/rubyblok/sitemap_config_generator.rb +40 -0
  11. data/lib/generators/rubyblok/webhook_controller_generator.rb +9 -7
  12. data/lib/generators/templates/carrier_wave_config.rb.erb +13 -0
  13. data/lib/generators/templates/hello_world_generator/_feature.html.erb +3 -0
  14. data/lib/generators/templates/hello_world_generator/_grid.html.erb +3 -0
  15. data/lib/generators/templates/hello_world_generator/_page.html.erb +1 -0
  16. data/lib/generators/templates/hello_world_generator/_teaser.html.erb +3 -0
  17. data/lib/generators/templates/hello_world_generator/controller.rb.erb +9 -0
  18. data/lib/generators/templates/hello_world_generator/index.html.erb +5 -0
  19. data/lib/generators/templates/hello_world_generator/styles.css +54 -0
  20. data/lib/generators/templates/image_cache_model.rb.erb +3 -0
  21. data/lib/generators/templates/image_cache_uploader.rb.erb +12 -0
  22. data/lib/generators/templates/migration_create_image_cache.rb.erb +12 -0
  23. data/lib/generators/templates/migration_update_image_cache.rb.erb +8 -0
  24. data/lib/generators/templates/rubyblok.rb +27 -6
  25. data/lib/generators/templates/sitemap.rb.erb +14 -0
  26. data/lib/rubyblok/configuration.rb +4 -3
  27. data/lib/rubyblok/helpers/storyblok_helper.rb +50 -17
  28. data/lib/rubyblok/mixins/model.rb +14 -0
  29. data/lib/rubyblok/mixins/webhook.rb +8 -4
  30. data/lib/rubyblok/railtie.rb +4 -2
  31. data/lib/rubyblok/services/get_storyblok_story.rb +5 -3
  32. data/lib/rubyblok/services/replace_storyblok_url.rb +64 -0
  33. data/lib/rubyblok/version.rb +1 -1
  34. data/lib/rubyblok.rb +17 -14
  35. data/rubyblok.gemspec +17 -17
  36. metadata +21 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '096e822e37774a75693902aff5fe81ccb971ed74d50e849a0b47fa49511642e6'
4
- data.tar.gz: 28ac8e653f9ed908a14be4622883acf6ef29c642d96925c1e84766228a5a0f1d
3
+ metadata.gz: 7012bc69cffbe4334c2176ae14a987af28f812c0a5587aecf579f13ce67e8b01
4
+ data.tar.gz: ace98ab94b1c2b10526c29fe24724d3cdcef12a76a172ea832d3c8ba38986d07
5
5
  SHA512:
6
- metadata.gz: feae7213dc1c38278accc0cc36ec91b903c15ac1087fb598e51551bf87156cdfe0a4eaaef033e7e3ff4c4ecd50a8296b874390c5b93db741b0fe69bda2efa474
7
- data.tar.gz: aa25681967a4c901c4c12c9146db6fd03d3f87ef39de957a47337f66a21cede5edd5b0350b61f9690530f376eb86d2f87d7cc17f2b7ad8c87d1895c95eeedfdf
6
+ metadata.gz: 8f420d367f94c54c3a2c7185e3785f72cbc7445e1323205bc962b6e48565f221c52ddcc534662864b7995ec8f1587dfd1ea915d4fe4479d507e61ae771562be9
7
+ data.tar.gz: c1f1266ef1eb592eb466bca2ba321bb1a7625248b2461e5696020234014f98d4d3d4d5b5ccbb3f234bcdcbf6689ff719c8bd093bc6b82446d4bc7d0b8fc6d87f
data/.rubocop.yml CHANGED
@@ -6,146 +6,7 @@ AllCops:
6
6
  NewCops: enable
7
7
  TargetRubyVersion: 3.1.X
8
8
 
9
- Capybara/VisibilityMatcher:
10
- Enabled: false
11
-
12
9
  Documentation:
13
10
  Enabled: false
14
-
15
- Layout/HashAlignment:
16
- Enabled: false
17
11
  Layout/LineLength:
18
12
  Max: 120
19
- Layout/MultilineMethodCallIndentation:
20
- EnforcedStyle: aligned
21
- Lint/AmbiguousBlockAssociation:
22
- Exclude:
23
- - spec/**/*
24
- Lint/IneffectiveAccessModifier:
25
- Enabled: false
26
- Lint/MissingSuper:
27
- Enabled: false
28
- Lint/RedundantSplatExpansion:
29
- Enabled: false
30
- Lint/UriEscapeUnescape:
31
- Enabled: false
32
- Metrics/BlockLength:
33
- Enabled: false
34
- Metrics/ClassLength:
35
- Max: 500
36
- Metrics/ModuleLength:
37
- Max: 250
38
- Exclude:
39
- - spec/**/*
40
- Naming/AccessorMethodName:
41
- Enabled: false
42
- Naming/BlockForwarding:
43
- Enabled: false
44
- Naming/InclusiveLanguage:
45
- Enabled: false
46
- Naming/FileName:
47
- Enabled: false
48
- Naming/MemoizedInstanceVariableName:
49
- Enabled: false
50
- Naming/PredicateName:
51
- Enabled: false
52
- Naming/VariableNumber:
53
- Enabled: false
54
- Style/ClassAndModuleChildren:
55
- Enabled: false
56
- Style/Documentation:
57
- Enabled: false
58
- Style/ExponentialNotation:
59
- Enabled: false
60
- Style/FetchEnvVar:
61
- Enabled: true
62
- Exclude:
63
- - spec/**/*
64
- Style/FormatString:
65
- Enabled: false
66
- Style/FormatStringToken:
67
- Enabled: false
68
- Style/FrozenStringLiteralComment:
69
- Enabled: false
70
- Style/HashSyntax:
71
- EnforcedStyle: ruby19
72
- Style/InPatternThen:
73
- Enabled: false
74
- Style/MultilineInPatternThen:
75
- Enabled: false
76
- Style/MapToHash:
77
- Enabled: false
78
- Style/NumericLiteralPrefix:
79
- Enabled: false
80
- Style/NumericPredicate:
81
- Enabled: false
82
- Style/OpenStructUse:
83
- Enabled: false
84
- Style/SafeNavigation:
85
- Enabled: false
86
- Style/StringLiterals:
87
- EnforcedStyle: double_quotes
88
- Style/TrailingCommaInArrayLiteral:
89
- EnforcedStyleForMultiline: comma
90
- Style/TrailingCommaInHashLiteral:
91
- EnforcedStyleForMultiline: comma
92
-
93
- RSpec/AnyInstance:
94
- Enabled: false
95
- RSpec/ChangeByZero:
96
- Enabled: false
97
- RSpec/ContextWording:
98
- Enabled: false
99
- RSpec/DescribedClass:
100
- Enabled: false
101
- RSpec/DescribeClass:
102
- Enabled: false
103
- RSpec/ExampleLength:
104
- Enabled: false
105
- RSpec/ExampleWording:
106
- Enabled: false
107
- RSpec/ExpectInHook:
108
- Enabled: false
109
- RSpec/HooksBeforeExamples:
110
- Enabled: false
111
- RSpec/ImplicitBlockExpectation:
112
- Enabled: false
113
- RSpec/InstanceVariable:
114
- Exclude:
115
- - spec/views/**/*
116
- RSpec/LetBeforeExamples:
117
- Enabled: false
118
- RSpec/LetSetup:
119
- Enabled: false
120
- RSpec/MessageChain:
121
- Enabled: false
122
- RSpec/MessageSpies:
123
- Enabled: false
124
- RSpec/MultipleDescribes:
125
- Enabled: false
126
- RSpec/MultipleExpectations:
127
- Enabled: false
128
- RSpec/MultipleMemoizedHelpers:
129
- Enabled: false
130
- RSpec/NamedSubject:
131
- Enabled: false
132
- RSpec/NestedGroups:
133
- Enabled: false
134
- RSpec/RepeatedDescription:
135
- Enabled: false
136
- RSpec/RepeatedExampleGroupBody:
137
- Enabled: false
138
- RSpec/RepeatedExampleGroupDescription:
139
- Enabled: false
140
- RSpec/StubbedMock:
141
- Enabled: false
142
- RSpec/VerifiedDoubles:
143
- Enabled: false
144
- RSpec/VerifiedDoubleReference:
145
- Enabled: false
146
- RSpec/FilePath:
147
- Enabled: false
148
- RSpec/SpecFilePathFormat:
149
- Enabled: false
150
- Style/FetchEnvVar:
151
- Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [1.2.0] - 2024-06-17
6
+
7
+ ### Added
8
+ - Caching Storyblok images on S3
9
+ - Fragment caching of Rubyblok components
10
+
11
+ ## [1.1.0] - 2024-05-02
12
+
13
+ ### Added
14
+ - Hello world generator
15
+
16
+ ## [1.0.0] - 2024-03-27
17
+
18
+ ### Added
19
+ - Initial release
20
+ - Rubyblok helpers
21
+ - Config generator
22
+ - Model generator for caching Storyblok content
23
+ - Webhook controller generator for publishing Storyblok contents
data/README.md CHANGED
@@ -8,123 +8,70 @@ This integration allows you to edit your content online, preview it in real-time
8
8
  In addition, Rubyblok provides an abstraction layer and stores all your content locally, reducing data usage and enhancing performance. This enables new functionalities that leverage the local data, such as global content search, sitemaps, and listings. Ultimately, this setup increases resilience by eliminating dependency on external data sources.
9
9
 
10
10
  ## Table of Contents
11
- 1. [Installation](#installation)
12
- 2. [Getting Started](#getting-started)
13
- 3. [Rubyblok tags](#rubyblok-tags)
14
- 4. [How to Run Tests](#how-to-run-tests)
15
- 5. [Guide for Contributing](#guide-for-contributing)
16
- 6. [How to Contact Us](#how-to-contact-us)
17
- 7. [License](#license)
11
+ 1. [Installation](#installation)
12
+ 2. [Getting Started](#getting-started)
13
+ 3. [Rubyblok tags](#rubyblok-tags)
14
+ 4. [Rubyblok workflows](#rubyblok-workflows)
15
+ 5. [Rubyblok webhook](#rubyblok-webhook)
16
+ 6. [Caching storyblok images](#caching-storyblok-images)
17
+ 7. [Caching views](#caching-views)
18
+ 8. [Sitemap configuration](#sitemap-configuration)
19
+ 9. [How to Run Tests](#how-to-run-tests)
20
+ 10. [Guide for Contributing](#guide-for-contributing)
21
+ 11. [How to Contact Us](#how-to-contact-us)
22
+ 12. [License](#license)
18
23
 
19
24
  ## Installation
20
25
  Rubyblok 1.0 works with Rails 6.0 onwards. Run:
21
- ```
26
+ ```bash
22
27
  bundle add rubyblok
23
28
  ```
24
29
 
25
- ### Storyblok account and variables
26
-
27
- [Click here](https://app.storyblok.com/?_gl=1*196uoul*_gcl_au*MTg1NjA5NjA0MS4xNzA5MDY5ODk3#!/signup) to create a free acount at Storyblok, the CMS platform where you will have access to the visual and real-time content editing.
30
+ ### Storyblok account and new space
31
+ [Click here](https://app.storyblok.com/#/signup) to create a free acount at Storyblok, the CMS platform where you will have access to the visual and real-time content editing.
28
32
 
29
- Create a new Space, in _Spaces > Add Space_.
33
+ Create a new Space, in _My Spaces > Add Space_. Select the free Community plan by clicking its "Continue" button and give your space a name.
30
34
 
31
35
  Get your Storyblok API token in your account, at _Storyblok Space > Settings > Access tokens_ page. Copy the "Preview" access level key.
32
36
 
33
- Add the key to your `STORYBLOK_API_TOKEN` in your env file, like this example:
37
+ Add the API token to your `config/initializers/rubyblok.rb` file:
34
38
 
35
39
  ```
36
- STORYBLOK_API_TOKEN=<your API token>
37
- ```
38
- You will also need to add the variables below to your env file:
39
- ```
40
- STORYBLOK_VERSION=draft
41
- STORYBLOK_WEBHOOK_SECRET=''
40
+ config.api_token = <your API token>
42
41
  ```
43
42
 
44
43
  ## Getting Started
45
44
 
46
- ### Your first Rubyblok page
47
- Let's get started with Rubyblok by creating our first page.
45
+ ### Hello world - Your first Rubyblok page
46
+ Let's get started with Rubyblok by creating your first page in three steps.
47
+ Note that it is important that you have your Storyblok space set up as described above, in the `Storyblok account and new space` section.
48
48
 
49
- First, you need to run the install generator, which will create the initializer for you:
49
+ 1. First, you need to run the install generator, which will create the initializer for you:
50
50
  ```bash
51
51
  rails g rubyblok:install
52
52
  ```
53
53
 
54
- Now let's generate and run a migration to create the `pages` table and the `Page` model:
54
+ 2. Now let's generate and run a migration to create the `pages` table and the `Page` model:
55
55
  ```bash
56
56
  rails g rubyblok:migration PAGE
57
57
 
58
58
  rails db:migrate
59
59
  ```
60
60
 
61
- Then, generate the webhook controller:
61
+ 3. Finally, let's generate your first page:
62
62
  ```bash
63
- rails g rubyblok:webhook_controller STORYBLOK_WEBHOOK
64
- ```
65
- It also adds this line to your `routes.rb` file:
66
- ```
67
- resources :storyblok_webhook, only: :create
63
+ rails g rubyblok:hello_world PAGE
68
64
  ```
69
- The Storyblok webhook is responsible for updating and deleting content in our local database in case of changes of content in Storyblok.
65
+ This will automatically create a new `/pages` route, a `PagesController`, views and styling for your hello world page.
70
66
 
71
- Finally, generate the home controller:
72
- ```bash
73
- rails g controller home_controller
74
- ```
75
- Add the following code to your home controller:
67
+ For this example, go to the `config/initializers/rubyblok.rb` file and turn the caching option off:
76
68
  ```
77
- def index
78
- response.headers['X-FRAME-OPTIONS'] = 'ALLOWALL'
79
- end
69
+ config.cached = false
80
70
  ```
81
71
 
82
- Add this code to your app/views/home/index.html.erb file:
83
- ```
84
- <%= rubyblok_story_tag('home') %>
85
- ```
86
- Configure your `routes.rb` file to call the home controller. For example, adding this line:
87
- ```
88
- root to: 'home#index'
89
- ```
72
+ Now you have created your first Hello World page! Start your Rails server, access the `/pages` route and you will be able to see the page.
90
73
 
91
- Create a `shared/storyblok` directory in the `views` directory, this directory is going to store the partials that render Storyblok components.
92
- You can change the folder settings at the `rubyblok.rb` file as needed:
93
- ```
94
- config.component_path = "shared/storyblok"
95
- ```
96
-
97
- Inside the `views/shared/storyblok` folder, create a file named `_page.html.erb` with the following code:
98
- ```
99
- <%= rubyblok_blocks_tag(blok.body) %>
100
- ```
101
-
102
- And then create another file for the hero section block `_hero_section.html.erb` (more explanation on that later):
103
- ```
104
- <section>
105
- <div>
106
- <%= rubyblok_content_tag(blok.headline) %>
107
- <%= rubyblok_content_tag(blok.subheadline) %>
108
- </div>
109
- </section>
110
- ```
111
-
112
- ### Creating your page at Storyblok
113
- 1. Once you're logged in, access your new space in the "My Spaces" section
114
- 2. Go to the "Content" section
115
- 3. Click the CTA "Create new" > Story
116
- 4. Name your story "Home", so it connects to our previous code. The content type is "Page".
117
- 5. Open your new story to start editing.
118
- 6. On the right side, you can add new blocks to your page. Create a new block by clicking the "+ Add Block" button.
119
- 7. This will open the Insert block section, then create the new "hero_section" block by typing its name in the search input.
120
- 8. Click the "Create new hero_section" CTA
121
- 9. Add the "headline" and "subheadline" text fields to the new Hero Section and save.
122
- 10. In your new Hero Section block, add any text you want to it.
123
- 11. Click the Publish button in the right top corner.
124
-
125
- Now you have your first demo page and block created. Start your rails server and you will be able to see it in your application.
126
-
127
- ### Activate the visual editor
74
+ ### Activate the visual editor
128
75
  Here are the steps to configure the visual editor at Storyblok. This allows you to see a preview of your changes in the Storyblok interface as you edit and save.
129
76
 
130
77
  At Storyblok, select your Space and go to _Settings > Visual Editor_.
@@ -155,7 +102,6 @@ This will start a proxy server.
155
102
 
156
103
  By doing this initial setup, you are able to see your first Storyblok page inside your app and edit its content in the Storyblok admin interface 🎉
157
104
 
158
-
159
105
  ## Rubyblok tags
160
106
 
161
107
  ### rubyblok_story_tag
@@ -164,7 +110,7 @@ Use this tag to render stories:
164
110
  # Slug: full_slug of the storyblok story
165
111
  <%= rubyblok_story_tag(slug) %>
166
112
  ```
167
- The name of the storyblok blok should match the rails partial, ie the `header` storyblok blok should have a corresponding `_header.html.erb` partial in the `config.component_path` directory. The partial is called with a `blok` local variable which contains the storyblok blok properties.
113
+ The name of the storyblok block should match the rails partial, ie if the block is named `header`, it should have a corresponding `_header.html.erb` partial in the directory defined in `config.component_path`. The partial is called with a `blok` local variable which contains the storyblok block properties.
168
114
 
169
115
  ### rubyblok_content_tag
170
116
  It renders content of Text, TextArea, Markdown or Richtext storyblok fields.
@@ -199,17 +145,100 @@ Use this tag to render more than one component:
199
145
  ```
200
146
 
201
147
  ### Updating content manually at the caching layer
202
-
203
- You can do the following in case you need to update the caching layer with some content that already exists in Storyblok:
148
+ In case you need to update the caching layer with new content added to Storyblok, run the following command:
204
149
  ```
205
150
  # Slug: full_slug of the storyblok story
206
- storyblok_story_content = Rubyblok::Services::GetStoryblokStory.call(slug: slug)
207
- <MODEL_NAME>.find_or_initialize_by(storyblok_story_slug: page)
208
- .update(storyblok_story_content:, storyblok_story_id: storyblok_story_content["id"])
151
+ storyblok_story = Rubyblok::Services::GetStoryblokStory.call(slug: slug)
152
+ <MODEL_NAME>.find_or_create(storyblok_story)
209
153
  ```
210
154
 
211
- ## How to Run Tests
155
+ ## Rubyblok workflows
212
156
 
157
+ ### Non-cached mode (default)
158
+ Rubyblok fetches the content via the Storyblok API and the content is not cached locally.
159
+
160
+ ### Cached mode
161
+ Rubyblok fetches the content from the local database. This mode is useful ie. if you don't want to call the API on every page request or you want to index the content locally. To enable this mode you need to set the cached feature on in the `config/initializers/rubyblok.rb` file:
162
+ ```
163
+ config.cached = true
164
+ ```
165
+
166
+ If you want to update the local cache on every page request (ie. the content is not updated via the webhook), you need to set the auto_update feature on in the `config/initializers/rubyblok.rb` file:
167
+ ```
168
+ config.auto_update = true
169
+ ```
170
+
171
+ ## Storyblok webhook
172
+ The Storyblok webhook will be responsible for updating and deleting content in the local database in case of changes. [Learn more here.](https://www.storyblok.com/docs/guide/in-depth/webhooks)
173
+
174
+ Generate the webhook controller:
175
+ ```bash
176
+ rails g rubyblok:webhook_controller STORYBLOK_WEBHOOK
177
+ ```
178
+
179
+ ## Caching storyblok images
180
+ You can store your storyblok images and videos on your own S3 storage by enabling this rubyblok feature.
181
+
182
+ 1. First, you need to run the image cache generator, which will create the model file, the uploader file and the carrierwave config file for you:
183
+ ```bash
184
+ rails g rubyblok:image_cache STORYBLOK_IMAGE
185
+ ```
186
+
187
+ 2. Add the following gems to your Gemfile:
188
+ ```
189
+ bundle add 'carrierwave'
190
+ bundle add 'fog-aws'
191
+
192
+ ```
193
+
194
+ 3. Now let's run a migration to create the `storyblok_images` table:
195
+ ```bash
196
+ rails db:migrate
197
+ ```
198
+
199
+ 4. Finally, enable the image cache feature in the `config/initializers/rubyblok.rb` file:
200
+ ```
201
+ config.use_cdn_images = true
202
+ ```
203
+
204
+ Please note that it caches only images added as an `Asset` field type in Storyblok.
205
+
206
+ ## Caching views
207
+
208
+ You can enable fragment caching on rubyblok components by setting the cache_views feature on in the `config/initializers/rubyblok.rb` file:
209
+ ```
210
+ config.cache_views = true
211
+ ```
212
+ Please note that if any of the component partials changes you need to clear the cache.
213
+
214
+ ## Sitemap configuration
215
+ You can generate a sitemap configuration for your website with the following command:
216
+ ```
217
+ rails g rubyblok:sitemap_config
218
+ ```
219
+ This generator will create a sitemap configuration for the `sitemap_generator` gem and add the gem to your Gemfile in case it's not already there.
220
+ The sitemap is generated only for cached content. Please make sure that the `cached` configuration value is `true` on `config/initializers/rubyblok.rb`.
221
+ Open `config/sitemap.rb` and add your hostname:
222
+ ```
223
+ # ...
224
+
225
+ # TODO: Configure your hostname here
226
+ SitemapGenerator::Sitemap.default_host = ''
227
+
228
+ # ...
229
+ ```
230
+ For example:
231
+ ```
232
+ # ...
233
+
234
+ # TODO: Configure your hostname here
235
+ SitemapGenerator::Sitemap.default_host = 'https://myhost.com'
236
+
237
+ # ...
238
+ ```
239
+ In order to generate your sitemap or customize this configuration, please read [the sitemap_generator gem documentation](https://github.com/kjvarga/sitemap_generator).
240
+
241
+ ## How to Run Tests
213
242
  You can run unit tests for RubyBlok with the following command:
214
243
  ```
215
244
  bundle exec rspec
@@ -223,5 +252,4 @@ Issues should be used to report bugs, request a new feature, or to discuss poten
223
252
  For any inquiries, reach out to us at: info@rubyblok.com
224
253
 
225
254
  ## License
226
-
227
255
  RubyBlok is released under the MIT License.
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
3
+ require 'bundler/gem_tasks'
4
4
  task default: %i[]
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Rubyblok
6
+ module Generators
7
+ class HelloWorldGenerator < Rails::Generators::NamedBase
8
+ source_root File.expand_path('../templates', __dir__)
9
+
10
+ desc 'Generates a "Hello, world" Rubyblok page.'
11
+
12
+ def generate_hello_world
13
+ add_controller
14
+ add_route
15
+ copy_views
16
+ end
17
+
18
+ private
19
+
20
+ def add_controller
21
+ template('hello_world_generator/controller.rb.erb', "app/controllers/#{plural_file_name}_controller.rb")
22
+ end
23
+
24
+ def add_route
25
+ insert_into_file('config/routes.rb', after: 'Rails.application.routes.draw do') do
26
+ "\n get '/#{plural_file_name}' => '#{plural_file_name}#index'\n"
27
+ end
28
+ end
29
+
30
+ def copy_views
31
+ template('hello_world_generator/index.html.erb', "app/views/#{plural_file_name}/index.html.erb")
32
+ template('hello_world_generator/_feature.html.erb', "app/views/#{destination_partial_path}/_feature.html.erb")
33
+ template('hello_world_generator/_page.html.erb', "app/views/#{destination_partial_path}/_page.html.erb")
34
+ template('hello_world_generator/_grid.html.erb', "app/views/#{destination_partial_path}/_grid.html.erb")
35
+ template('hello_world_generator/_teaser.html.erb', "app/views/#{destination_partial_path}/_teaser.html.erb")
36
+
37
+ copy_file('hello_world_generator/styles.css', 'app/assets/stylesheets/hello.css')
38
+ end
39
+
40
+ def destination_partial_path
41
+ @destination_partial_path ||= Rubyblok.configuration.component_path
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Rubyblok
7
+ module Generators
8
+ class ImageCacheGenerator < ::Rails::Generators::NamedBase
9
+ include ::Rails::Generators::Migration
10
+ source_root File.expand_path('../templates', __dir__)
11
+ desc 'Installs image cache migration and model files.'
12
+
13
+ def install
14
+ if table_exist?
15
+ migration_template('migration_update_image_cache.rb.erb', "db/migrate/update_#{plural_file_name}.rb",
16
+ migration_version:)
17
+ else
18
+ migration_template('migration_create_image_cache.rb.erb', "db/migrate/create_#{plural_file_name}.rb",
19
+ migration_version:)
20
+ end
21
+
22
+ create_or_update_model
23
+ add_model_name_to_config
24
+ end
25
+
26
+ def self.next_migration_number(dirname)
27
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
28
+ end
29
+
30
+ private
31
+
32
+ def migration_version
33
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
34
+ end
35
+
36
+ def table_exist?
37
+ ActiveRecord::Base.connection.table_exists?(plural_file_name.to_sym)
38
+ end
39
+
40
+ def create_or_update_model
41
+ if table_exist?
42
+ add_mixins_to_existing_model
43
+ else
44
+ generate_new_model_from_template
45
+ end
46
+ end
47
+
48
+ def add_mixins_to_existing_model
49
+ model_path = "app/models/#{file_name}.rb"
50
+ mixins_code = "\n mount_uploader :image, #{class_name}Uploader\n"
51
+
52
+ insert_into_file model_path, after: "class #{class_name} < ApplicationRecord" do
53
+ mixins_code
54
+ end
55
+
56
+ add_uploader_and_config
57
+ end
58
+
59
+ def generate_new_model_from_template
60
+ template('image_cache_model.rb.erb', "app/models/#{file_name}.rb")
61
+ add_uploader_and_config
62
+ end
63
+
64
+ def add_uploader_and_config
65
+ template('image_cache_uploader.rb.erb', "app/uploaders/#{file_name}_uploader.rb")
66
+ template('carrier_wave_config.rb.erb', 'config/initializers/carrier_wave.rb')
67
+ end
68
+
69
+ def add_model_name_to_config
70
+ model_path = 'config/initializers/rubyblok.rb'
71
+ insert_into_file model_path, after: 'config.image_model_name = "' do
72
+ class_name
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,15 +1,17 @@
1
- require "rails/generators"
2
- require "rails/generators/active_record"
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
3
5
 
4
6
  module Rubyblok
5
7
  module Generators
6
8
  class InstallGenerator < Rails::Generators::Base
7
- source_root File.expand_path("../templates", __dir__)
9
+ source_root File.expand_path('../templates', __dir__)
8
10
 
9
- desc "Generates an initializer file."
11
+ desc 'Generates an initializer file.'
10
12
 
11
13
  def copy_initializer
12
- template("rubyblok.rb", "config/initializers/rubyblok.rb")
14
+ template('rubyblok.rb', 'config/initializers/rubyblok.rb')
13
15
  end
14
16
  end
15
17
  end
@@ -1,19 +1,21 @@
1
- require "rails/generators"
2
- require "rails/generators/active_record"
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
3
5
 
4
6
  module Rubyblok
5
7
  module Generators
6
8
  class MigrationGenerator < ::Rails::Generators::NamedBase
7
9
  include ::Rails::Generators::Migration
8
- source_root File.expand_path("../templates", __dir__)
9
- desc "Installs Rubyblok migration and model files."
10
+ source_root File.expand_path('../templates', __dir__)
11
+ desc 'Installs Rubyblok migration and model files.'
10
12
 
11
13
  def install
12
14
  if table_exist?
13
- migration_template("migration_update.rb.erb", "db/migrate/update_rubyblok_#{plural_file_name}.rb",
15
+ migration_template('migration_update.rb.erb', "db/migrate/update_rubyblok_#{plural_file_name}.rb",
14
16
  migration_version:)
15
17
  else
16
- migration_template("migration_create.rb.erb", "db/migrate/create_rubyblok_#{plural_file_name}.rb",
18
+ migration_template('migration_create.rb.erb', "db/migrate/create_rubyblok_#{plural_file_name}.rb",
17
19
  migration_version:)
18
20
  end
19
21
 
@@ -53,11 +55,11 @@ module Rubyblok
53
55
  end
54
56
 
55
57
  def generate_new_model_from_template
56
- template("model.rb.erb", "app/models/#{file_name}.rb")
58
+ template('model.rb.erb', "app/models/#{file_name}.rb")
57
59
  end
58
60
 
59
61
  def add_model_name_to_config
60
- model_path = "config/initializers/rubyblok.rb"
62
+ model_path = 'config/initializers/rubyblok.rb'
61
63
  insert_into_file model_path, after: 'config.model_name = "' do
62
64
  class_name
63
65
  end