mozaic 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35980466fff36ddb7ab0a0454f497cdde5d9665abfaf2447d427376173134fd2
4
+ data.tar.gz: f9260f944122932705d9767deb77119cb92342cc650b23082cbc73eeda26577a
5
+ SHA512:
6
+ metadata.gz: 273f13503afde1563c9552ea1f0be5b4546f22f89457cc02df25d85a4b578be6e6c651f35074f0689c95230c4faaa5b38cab9312937136202247923dc82bc9f0
7
+ data.tar.gz: 337c765c07700f0b43864878bf6d21289373c9e2a16284e59625ee49b432f8c9b652f1f26134edef524cbda8a2ae28d03e0da980c7da2d19612ad03d2c073347
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ### master
4
+
5
+ * nothing yet
6
+
7
+ ### 1.0.0 - 2018/01/10
8
+
9
+ * initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Jonas Hübotter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,210 @@
1
+ # Mozaic
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/mozaic.svg)](https://badge.fury.io/rb/mozaic) <img src="https://travis-ci.org/jonhue/mozaic.svg?branch=master" />
4
+
5
+ Mozaic is an opinionated layout engine for Rails. It simplifies handling numerous layouts and components.
6
+
7
+ ---
8
+
9
+ ## Table of Contents
10
+
11
+ * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [Layouts](#layouts)
14
+ * [Components](#components)
15
+ * [Configuration](#configuration)
16
+ * [To Do](#to-do)
17
+ * [Contributing](#contributing)
18
+ * [Contributors](#contributors)
19
+ * [Semantic versioning](#semantic-versioning)
20
+ * [License](#license)
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ Mozaic works with Rails 5 onwards. You can add it to your `Gemfile` with:
27
+
28
+ ```ruby
29
+ gem 'mozaic'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install mozaic
39
+
40
+ If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
41
+
42
+ ```ruby
43
+ gem 'mozaic', github: 'jonhue/mozaic'
44
+ ```
45
+
46
+ Now run the generator:
47
+
48
+ $ rails g mozaic:install
49
+
50
+ ---
51
+
52
+ ## Usage
53
+
54
+ ### Layouts
55
+
56
+ You can run a generator to create new layouts
57
+
58
+ $ rails g mozaic:layout -n my/new/layout
59
+
60
+ **Note:** Pass the `-e` option to extend another layout with this new layout.
61
+
62
+ You can use the `mozaic` helper to extend other layouts:
63
+
64
+ ```haml
65
+ = mozaic :layout do
66
+ ...
67
+ ```
68
+
69
+ `mozaic` takes the layout you want to extend as a first and the area where content should be rendered as a second parameter. Both of them default to `:mozaic`.
70
+
71
+ Here is how you specify areas to extend layouts:
72
+
73
+ ```haml
74
+ = mozaic_area :area do
75
+ ...
76
+ ```
77
+
78
+ **Note:** Make sure that your area names are unique across all layouts.
79
+
80
+ ### Components
81
+
82
+ Mozaic allows you to define components to organize your code:
83
+
84
+ $ rails g mozaic:component -n name
85
+
86
+ ```ruby
87
+ Mozaic.configure do |config|
88
+ config.define_component :name
89
+ end
90
+ ```
91
+
92
+ You are also able to pass default options to your components:
93
+
94
+ ```ruby
95
+ Mozaic.configure do |config|
96
+ config.define_component :name, lovely: true
97
+ end
98
+ ```
99
+
100
+ ```haml
101
+ = options[:lovely].to_s
102
+ ```
103
+
104
+ You can run custom code whenever your component gets rendered:
105
+
106
+ ```ruby
107
+ Mozaic.configure do |config|
108
+ config.define_component :name, lovely: true do
109
+ options[:lovely] = !options[:lovely]
110
+ end
111
+ end
112
+ ```
113
+
114
+ Components can also be defined in your ActiveRecord classes:
115
+
116
+ ```ruby
117
+ class User < ApplicationRecord
118
+ define_component :avatar
119
+ end
120
+ ```
121
+
122
+ Rendering components is fairly straight forward:
123
+
124
+ ```haml
125
+ = component :name
126
+ ```
127
+
128
+ You are able to override the default options:
129
+
130
+ ```haml
131
+ = component :name, lovely: false
132
+ ```
133
+
134
+ And to pass a block:
135
+
136
+ ```haml
137
+ = component :name, lovely: false do
138
+ It is lovely.
139
+ ```
140
+
141
+ Here is how to access a block in your component:
142
+
143
+ ```haml
144
+ = options[:lovely].to_s
145
+ = block
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Configuration
151
+
152
+ You can configure Mozaic by passing a block to `configure`. This can be done in `config/initializers/mozaic.rb`:
153
+
154
+ ```ruby
155
+ Mozaic.configure do |config|
156
+ config.define_component :name
157
+ end
158
+ ```
159
+
160
+ * `define_component` Define components
161
+
162
+ ---
163
+
164
+ ## To Do
165
+
166
+ [Here](https://github.com/jonhue/mozaic/projects/1) is the full list of current projects.
167
+
168
+ To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/mozaic/issues/new).
169
+
170
+ ---
171
+
172
+ ## Contributing
173
+
174
+ We hope that you will consider contributing to Mozaic. Please read this short overview for some information about how to get started:
175
+
176
+ [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
177
+
178
+ ### Contributors
179
+
180
+ Give the people some :heart: who are working on this project. See them all at:
181
+
182
+ https://github.com/jonhue/mozaic/graphs/contributors
183
+
184
+ ### Semantic Versioning
185
+
186
+ Mozaic follows Semantic Versioning 2.0 as defined at http://semver.org.
187
+
188
+ ## License
189
+
190
+ MIT License
191
+
192
+ Copyright (c) 2018 Jonas Hübotter
193
+
194
+ Permission is hereby granted, free of charge, to any person obtaining a copy
195
+ of this software and associated documentation files (the "Software"), to deal
196
+ in the Software without restriction, including without limitation the rights
197
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
198
+ copies of the Software, and to permit persons to whom the Software is
199
+ furnished to do so, subject to the following conditions:
200
+
201
+ The above copyright notice and this permission notice shall be included in all
202
+ copies or substantial portions of the Software.
203
+
204
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
205
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
206
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
207
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
208
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
209
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
210
+ SOFTWARE.
@@ -0,0 +1,17 @@
1
+ module Mozaic
2
+ module ComponentHelper
3
+
4
+ def component name, options = {}, &block
5
+ component = Mozaic::Component.find_by_name(name.to_sym).first
6
+ component.render options
7
+ render partial: "mozaic/#{name.to_s}", locals: { options: component.options(options), block: ( block_given? ? capture(&block) : nil ) }
8
+ end
9
+
10
+ def component_wrapper name, options = {}, &block
11
+ content_tag class: "#{name.to_s.split('/').join(' ')} #{options[:class]}", id: options[:id] do
12
+ capture(&block)
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Mozaic
2
+ module LayoutHelper
3
+
4
+ def mozaic parent = :mozaic, area = :mozaic, &block
5
+ extends parent.to_sym do
6
+ replace area.to_sym do
7
+ capture(&block)
8
+ end
9
+ end
10
+ end
11
+
12
+ def mozaic_area name = :mozaic, &block
13
+ if block_given?
14
+ area name.to_sym do
15
+ capture(&block)
16
+ end
17
+ else
18
+ area name.to_sym
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Mozaic
5
+ class ComponentGenerator < Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.join File.dirname(__FILE__), '../templates/component'
10
+ desc 'Generate a Mozaic component'
11
+
12
+ class_option :name, desc: 'Component name', type: :string, aliases: '-n'
13
+
14
+ def create_files
15
+ names = options[:name].split('/')
16
+ name = names.pop
17
+ template 'partial.html.erb', "app/views/mozaic/#{names.join('/')}/_#{name}.html.erb"
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Mozaic
5
+ class InstallGenerator < Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.join File.dirname(__FILE__), '../templates/install'
10
+ desc 'Install Mozaic'
11
+
12
+ def create_initializer
13
+ template 'initializer.rb', 'config/initializers/mozaic.rb'
14
+ end
15
+
16
+ def create_layout
17
+ template 'layout.html.erb', 'app/views/layouts/mozaic.html.erb'
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Mozaic
5
+ class LayoutGenerator < Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.join File.dirname(__FILE__), '../templates/layout'
10
+ desc 'Generate a Mozaic layout'
11
+
12
+ class_option :name, desc: 'Layout name', type: :string, aliases: '-n'
13
+ class_option :extends, desc: 'Layout this layout is extending', type: :string, aliases: '-e'
14
+
15
+ def create_files
16
+ names = options[:name].split('/')
17
+ name = names.pop
18
+ template 'layout.html.erb', "app/views/layouts/#{names.join('/')}/#{name].html.erb}"
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ <%%= component_wrapper '<%= options[:name] %>' do %>
2
+ <%% end %>
@@ -0,0 +1,6 @@
1
+ Mozaic.configure do |config|
2
+
3
+ # Define Mozaic components
4
+ # config.define_component :name
5
+
6
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title></title>
5
+ <%%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': true %>
6
+ <%%= javascript_include_tag 'application', 'data-turbolinks-track': true %>
7
+ <%%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <%%= mozaic_area do %>
11
+ <%%= yield %>
12
+ <%% end %>
13
+ </body>
14
+ </html>
@@ -0,0 +1,2 @@
1
+ <%%= mozaic<%= " '#{options[:extends]}'" if options.has_key?(:extends) %> do %>
2
+ <%% end %>
@@ -0,0 +1,13 @@
1
+ require 'mozaic/version'
2
+
3
+ module Mozaic
4
+
5
+ require 'mozaic/configuration'
6
+
7
+ require 'mozaic/component'
8
+ require 'mozaic/component/define'
9
+
10
+ require 'mozaic/engine'
11
+ require 'mozaic/railtie'
12
+
13
+ end
@@ -0,0 +1,32 @@
1
+ module Mozaic
2
+ class Component
3
+
4
+ cattr_accessor :instances
5
+ attr_accessor :name
6
+ attr_accessor :block
7
+ attr_writer :options
8
+
9
+ def initialize name, options = {}, &block
10
+ self.name = name.to_sym
11
+ self.block = block if block_given?
12
+ self.options = options
13
+ self.class.instances = [] if @@instances.nil?
14
+ self.class.instances << self
15
+ end
16
+
17
+ def options options = {}
18
+ @options.merge! options
19
+ end
20
+
21
+ def render options = {}
22
+ self.options = self.options options
23
+ self.block.call unless self.block.nil?
24
+ end
25
+
26
+ def self.find_by_name name
27
+ return [] if self.instances.nil?
28
+ self.instances.select { |component| component.name == name.to_sym }
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module Mozaic
2
+ class Component
3
+ module Define
4
+
5
+ def define_component name, options = {}
6
+ Mozaic.configure do |config|
7
+ if block_given?
8
+ config.define_component name.to_sym, options, &Proc.new
9
+ else
10
+ config.define_component name.to_sym, options
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ module Mozaic
2
+
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configure
8
+ self.configuration ||= Configuration.new
9
+ yield configuration
10
+ end
11
+
12
+ class Configuration
13
+
14
+ def define_component name, options = {}
15
+ if block_given?
16
+ Mozaic::Component.new name.to_sym, options, &Proc.new
17
+ else
18
+ Mozaic::Component.new name.to_sym, options
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails/railtie'
2
+
3
+ module Mozaic
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/railtie'
2
+ require 'active_support'
3
+
4
+ module Mozaic
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer 'mozaic.initialize' do
8
+ ActiveSupport.on_load :active_record do
9
+ include Mozaic::Component::Define
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Mozaic
2
+
3
+ VERSION = '1.0.0'
4
+
5
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mozaic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Hübotter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nestive-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.52'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.52'
83
+ description: Mozaic is an opinionated layout engine for Rails. It simplifies handling
84
+ numerous layouts and components.
85
+ email: me@jonhue.me
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CHANGELOG.md
91
+ - LICENSE
92
+ - README.md
93
+ - app/helpers/mozaic/component_helper.rb
94
+ - app/helpers/mozaic/layout_helper.rb
95
+ - lib/generators/mozaic/component_generator.rb
96
+ - lib/generators/mozaic/install_generator.rb
97
+ - lib/generators/mozaic/layout_generator.rb
98
+ - lib/generators/templates/component/partial.html.erb
99
+ - lib/generators/templates/install/initializer.rb
100
+ - lib/generators/templates/install/layout.html.erb
101
+ - lib/generators/templates/layout/layout.html.erb
102
+ - lib/mozaic.rb
103
+ - lib/mozaic/component.rb
104
+ - lib/mozaic/component/define.rb
105
+ - lib/mozaic/configuration.rb
106
+ - lib/mozaic/engine.rb
107
+ - lib/mozaic/railtie.rb
108
+ - lib/mozaic/version.rb
109
+ homepage: https://github.com/jonhue/mozaic
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '2.3'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.7.4
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Mozaic is an opinionated layout engine for Rails
133
+ test_files: []