greased-rails 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .project
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in greased-rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Joel Van Horn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # GreasedRails
2
+
3
+ Reusable default application settings, environment variables, and deployment tasks.
4
+
5
+ Replicate common Rails application settings and environment variables using templates. Handy deployment tasks make managing your environments easier.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'greased-rails'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install greased-rails
20
+
21
+ ## Usage
22
+
23
+ There are two configuration files you can create (optional):
24
+
25
+ * greased_settings.yml (serialized application settings)
26
+ * greased_variables.yml (serialized ENV variables)
27
+
28
+ You can see what they look like in the "examples" folder.
29
+
30
+ ### Application Settings - greased_settings.yml
31
+
32
+ This is a YAML serialization of most settings that you would find in "/app/config/application.rb" and "config/environments/*.rb". What's nice is that you can easily view settings for each environment in one file and allow them to inherit from eachother.
33
+
34
+ You can save your "greased_settings.yaml" file in the root of your Rails application or in the "config" folder. If you don't create your own file, Greased will use the file in the "examples" folder of this gem.
35
+
36
+ ### Environment Variables - greased_variables.yml
37
+
38
+ This is a YAML serialization of your environment variables. You can easily share environment variables across environments.
39
+
40
+ You can save your "greased_settings.yaml" file in the root of your Rails application or in the "config" folder. If you don't create your own file, Greased won't load any environment variables.
41
+
42
+ You can use the "greased:env:dump" Rake task to create *.env files for "development", "staging", and "production". These files can be used by Foreman to load environment variables when you start your Rails server.
43
+
44
+ (Read more about Foreman and environment variables: http://joelvanhorn.com/2012/06/12/developing-apps-with-environment-variables/)
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,275 @@
1
+ defaults: &defaults
2
+ config:
3
+ force_ssl: false
4
+ autoload_paths +=:
5
+ - "<%= Rails.root %>/lib"
6
+ # See everything in the log (default is :info)
7
+ log_level: :debug
8
+ # Settings in config/environments/* take precedence over those specified here.
9
+ # Application configuration should go into files in config/initializers
10
+ # -- all .rb files in that directory are automatically loaded.
11
+
12
+ # Custom directories with classes and modules you want to be autoloadable.
13
+ # config.autoload_paths += %W(#{config.root}/extras)
14
+
15
+ # Only load the plugins named here, in the order given (default is alphabetical).
16
+ # :all can be used as a placeholder for all plugins not explicitly named.
17
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
18
+
19
+ # Activate observers that should always be running.
20
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
21
+
22
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
23
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
24
+ time_zone: 'Eastern Time (US & Canada)'
25
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
26
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
27
+ # config.i18n.default_locale = :de
28
+
29
+ # Configure the default encoding used in templates for Ruby 1.9.
30
+ encoding: 'utf-8'
31
+ # Configure sensitive parameters which will be filtered from the log file.
32
+ filter_parameters +=:
33
+ - :password
34
+ # Enable escaping HTML in JSON.
35
+ active_support:
36
+ escape_html_entities_in_json: true
37
+ # Use SQL instead of Active Record's schema dumper when creating the database.
38
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
39
+ # like if you have constraints or database-specific column types
40
+ # config.active_record.schema_format = :sql
41
+
42
+ # Enforce whitelist mode for mass assignment.
43
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
44
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
45
+ # parameters by using an attr_accessible or attr_protected declaration.
46
+ active_record:
47
+ whitelist_attributes: true
48
+ action_mailer:
49
+ default_url_options:
50
+ :host: "<%= ENV['APP_HOST'] %>"
51
+ asset_host: "http://<%= ENV['ASSET_HOST'] %>"
52
+ smtp_settings:
53
+ :address: "<%= ENV['SENDGRID_SMTP'] %>"
54
+ :port: "<%= ENV['SENDGRID_SMTP_PORT'] %>"
55
+ :authentication: :plain
56
+ :user_name: "<%= ENV['SENDGRID_USERNAME'] %>"
57
+ :password: "<%= ENV['SENDGRID_PASSWORD'] %>"
58
+ :domain: "<%= ENV['SENDGRID_DOMAIN'] %>"
59
+ action_controller:
60
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
61
+ asset_host: "//<%= ENV['ASSET_HOST'] %>"
62
+ assets:
63
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
64
+ precompile +=:
65
+ - "rails_admin.js"
66
+ - "rails_admin.css"
67
+ - "rails_admin/rails_admin.js"
68
+ - "rails_admin/rails_admin.css"
69
+ - "rails_admin/jquery.colorpicker.js"
70
+ - "rails_admin/jquery.colorpicker.css"
71
+ initialize_on_precompile: false
72
+ # Enable the asset pipeline
73
+ enabled: true
74
+ js_compressor: :closure
75
+ #css_compressor: :less
76
+ # Version of your assets, change this if you want to expire all your assets
77
+ version: '1.0'
78
+ # if defined? ::Rack::PerftoolsProfiler
79
+ # config.middleware.use ::Rack::PerftoolsProfiler, :default_printer => 'gif', :bundler => true
80
+ # end
81
+ # memory profiling
82
+ # if defined?(Oink) && defined?(ActiveRecord)
83
+ # config.middleware.use Oink::Middleware, :logger => Logger.new(STDOUT)#Rails.logger
84
+ # end
85
+ # # error notifications
86
+ # config.middleware.use ExceptionNotifier,
87
+ # # new feature in future release
88
+ # #:email_format => :html#,
89
+ # :email_prefix => "[#{ENV['APP_NAME']} #{Rails.env.titleize} Error] ",
90
+ # :sender_address => %{"#{ENV['APP_NAME']} Notifier" <#{Rails.env}@#{ENV['PRIMARY_DOMAIN']}>},
91
+ # :exception_recipients => [ ENV['SHADOW_EMAIL'] ],
92
+ # :ignore_if => lambda { |env, e| Rails.env.development? }
93
+ # # customize mailer layouts
94
+ # config.to_prepare do
95
+ # Devise::Mailer.layout "mailer" # email.haml or email.erb
96
+ # #Devise::Mailer.helper :mailer
97
+ # end
98
+ # unless Rails.env.development?
99
+ # # gzip assets
100
+ # use Rack::Deflater
101
+ #
102
+ # # CORS
103
+ #
104
+ # #require 'rack/cors'
105
+ #
106
+ # use Rack::Cors do
107
+ # allow do
108
+ # origins '*'
109
+ # resource '/assets/*', :headers => :any, :methods => :get
110
+ # end
111
+ # end
112
+ # end
113
+ routes:
114
+ default_url_options:
115
+ :host: "<%= ENV['APP_HOST'] %>"
116
+
117
+ development:
118
+ <<: *defaults
119
+ # config.after_initialize do
120
+ # Bullet.enable = true
121
+ # Bullet.alert = true
122
+ # Bullet.bullet_logger = true
123
+ # Bullet.console = true
124
+ # # Bullet.growl = true
125
+ # # Bullet.xmpp = {
126
+ # # :account => 'bullets_account@jabber.org',
127
+ # # :password => 'bullets_password_for_jabber',
128
+ # # :receiver => 'your_account@jabber.org',
129
+ # # :show_online_status => true
130
+ # # }
131
+ # Bullet.rails_logger = true
132
+ # Bullet.airbrake = false
133
+ # Bullet.disable_browser_cache = true
134
+ # end
135
+ config:
136
+ # In the development environment your application's code is reloaded on
137
+ # every request. This slows down response time but is perfect for development
138
+ # since you don't have to restart the web server when you make code changes.
139
+ cache_classes: false
140
+ # Log error messages when you accidentally call methods on nil.
141
+ whiny_nils: true
142
+ # Show full error reports and disable caching
143
+ consider_all_requests_local: true
144
+ action_controller:
145
+ perform_caching: false
146
+ action_mailer:
147
+ # Disable delivery errors, bad email addresses will be ignored
148
+ raise_delivery_errors: false
149
+ # Use a different cache store in production
150
+ cache_store: :null_store
151
+ active_support:
152
+ # Print deprecation notices to the Rails logger
153
+ deprecation: :log
154
+ action_dispatch:
155
+ # Specifies the header that your server uses for sending files
156
+ # x_sendfile_header: "X-Sendfile" # for apache
157
+ # x_sendfile_header: 'X-Accel-Redirect' # for nginx
158
+
159
+ # Only use best-standards-support built into browsers
160
+ best_standards_support: :builtin
161
+ active_record:
162
+ # Raise exception on mass assignment protection for Active Record models
163
+ mass_assignment_sanitizer: :strict
164
+ # Log the query plan for queries taking more than this (works
165
+ # with SQLite, MySQL, and PostgreSQL)
166
+ auto_explain_threshold_in_seconds: 0.5
167
+ assets:
168
+ # Do not compress assets
169
+ compress: false
170
+ # Expands the lines which load the assets
171
+ debug: false
172
+
173
+ staging:
174
+ <<: *defaults
175
+ config:
176
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
177
+ force_ssl: false
178
+ lograge:
179
+ # more concise logging (one line per request)
180
+ enabled: false
181
+ # Code is not reloaded between requests
182
+ cache_classes: true
183
+ # Full error reports are disabled and caching is turned on
184
+ consider_all_requests_local: false
185
+ action_controller:
186
+ perform_caching: true
187
+ # Disable Rails's static asset server (Apache or nginx will already do this)
188
+ serve_static_assets: true
189
+ static_cache_control: 'public, max-age=2592000'
190
+ assets:
191
+ # Compress JavaScripts and CSS
192
+ compress: true
193
+ # Don't fallback to assets pipeline if a precompiled asset is missed
194
+ compile: false
195
+ # Generate digests for assets URLs
196
+ digest: true
197
+ # Defaults to nil and saved in location specified by config.assets.prefix
198
+ # manifest: YOUR_PATH
199
+
200
+ # See everything in the log (default is :info)
201
+ log_level: :debug
202
+ # Prepend all log lines with the following tags
203
+ # log_tags: [ :subdomain, :uuid ]
204
+
205
+ # Use a different logger for distributed setups
206
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
207
+
208
+ # Use a different cache store in production
209
+ cache_store: :dalli_store
210
+
211
+ # config.action_dispatch.rack_cache = {
212
+ # :metastore => Dalli::Client.new,
213
+ # :entitystore => 'file:tmp/cache/rack/body',
214
+ # :allow_reload => false
215
+ # }
216
+ #config.threadsafe!
217
+ i18n:
218
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
219
+ # the I18n.default_locale when a translation can not be found)
220
+ fallbacks: true
221
+ active_support:
222
+ # Send deprecation notices to registered listeners
223
+ deprecation: :notify
224
+
225
+ production:
226
+ <<: *defaults
227
+ config:
228
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
229
+ force_ssl: false
230
+ lograge:
231
+ # more concise logging (one line per request)
232
+ enabled: true
233
+ # Code is not reloaded between requests
234
+ cache_classes: true
235
+ # Full error reports are disabled and caching is turned on
236
+ consider_all_requests_local: false
237
+ action_controller:
238
+ perform_caching: true
239
+ # Disable Rails's static asset server (Apache or nginx will already do this)
240
+ serve_static_assets: true
241
+ static_cache_control: 'public, max-age=2592000'
242
+ assets:
243
+ # Compress JavaScripts and CSS
244
+ compress: true
245
+ # Don't fallback to assets pipeline if a precompiled asset is missed
246
+ compile: false
247
+ # Generate digests for assets URLs
248
+ digest: true
249
+ # Defaults to nil and saved in location specified by config.assets.prefix
250
+ # manifest: YOUR_PATH
251
+
252
+ # See everything in the log (default is :info)
253
+ log_level: :debug
254
+ # Prepend all log lines with the following tags
255
+ # log_tags: [ :subdomain, :uuid ]
256
+
257
+ # Use a different logger for distributed setups
258
+ # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
259
+
260
+ # config.action_dispatch.rack_cache = {
261
+ # :metastore => Dalli::Client.new,
262
+ # :entitystore => 'file:tmp/cache/rack/body',
263
+ # :allow_reload => false
264
+ # }
265
+ #config.threadsafe!
266
+ i18n:
267
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
268
+ # the I18n.default_locale when a translation can not be found)
269
+ fallbacks: true
270
+ # Use a different cache store in production
271
+ cache_store: :dalli_store
272
+ active_support:
273
+ # Send deprecation notices to registered listeners
274
+ deprecation: :notify
275
+
@@ -0,0 +1,49 @@
1
+ defaults: &defaults
2
+ deployment:
3
+ HEROKU_APP: my-app-name
4
+ HEROKU_USERNAME: example@example.com
5
+ HEROKU_PASSWORD: *******
6
+
7
+ GIT_REMOTE: heroku
8
+ GIT_BRANCH: master
9
+
10
+ application:
11
+ COMPANY_NAME: Acme, Inc.
12
+ APP_NAME: Acme Online
13
+ APP_TITLE: The Official Acme Website
14
+
15
+ TZ: America/New_York
16
+
17
+ SUPERADMINS: admin1@acme.com,admin2@acme.com
18
+ SENDER_NAME: Acme
19
+ SENDER_EMAIL: sender@acme.com
20
+ SHADOW_EMAIL: tech@acme.com
21
+ ENABLE_EMAILS: false
22
+
23
+ SENDGRID_USERNAME: acme
24
+ SENDGRID_PASSWORD: *******
25
+ SENDGRID_DOMAIN: acme.com
26
+ SENDGRID_SMTP: smtp.sendgrid.net
27
+ SENDGRID_SMTP_PORT: 587
28
+
29
+ development:
30
+ <<: *defaults
31
+ application:
32
+ PRIMARY_DOMAIN: acme.local
33
+ APP_HOST: acme.local:3000
34
+ ASSET_HOST: acme.local:3000
35
+
36
+ LINKEDIN_KEY: *******
37
+ LINKEDIN_SECRET: *******
38
+
39
+ production:
40
+ <<: *defaults
41
+ application:
42
+ PRIMARY_DOMAIN: acme.com
43
+ APP_HOST: acme.com
44
+ ASSET_HOST: acme.com
45
+
46
+ ENABLE_EMAILS: true
47
+
48
+ LINKEDIN_KEY: *******
49
+ LINKEDIN_SECRET: *******
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'greased/rails/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "greased-rails"
8
+ gem.version = Greased::Rails::VERSION
9
+ gem.authors = ["Joel Van Horn"]
10
+ gem.email = ["joel@joelvanhorn.com"]
11
+ gem.description = %q{Reusable default application settings, environment variables, and deployment tasks.}
12
+ gem.summary = %q{Replicate common Rails application settings and environment variables using templates. Handy deployment tasks make managing your environments easier.}
13
+ gem.homepage = "http://rubygems.org/gems/greased-rails"
14
+
15
+ gem.add_dependency "activesupport", ">= 3.2.0"
16
+ gem.add_dependency "railties", ">= 3.2.0"
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib", "tasks"]
22
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "greased/rails"
2
+ require_relative "greased/applicator"
3
+ require_relative "greased/setting"
4
+ require_relative "greased/settings"
@@ -0,0 +1,114 @@
1
+ require 'delegate'
2
+ require 'pathname'
3
+ require 'active_support/core_ext/hash/deep_merge'
4
+
5
+ module Greased
6
+ class Applicator
7
+
8
+ APP_SETTINGS_FILENAME = "greased_settings.yml"
9
+ ENV_VARS_FILENAME = "greased_variables.yml"
10
+ DEFAULT_SETTINGS_FILE = Pathname.new(File.join(File.dirname(__FILE__), '../../examples', APP_SETTINGS_FILENAME)).realpath
11
+ DEFAULT_ENV = "development"
12
+
13
+ def self.default_options
14
+ {
15
+ :app => nil,
16
+ :env => ENV['RAILS_ENV'] || ENV['RACK_ENV'] || DEFAULT_ENV,
17
+ :groups => ["application"],
18
+ :app_filename => DEFAULT_SETTINGS_FILE,
19
+ #:env_filename => [],
20
+ :skip_erb => false
21
+ }
22
+ end
23
+
24
+ def self.rails_options
25
+ if defined? ::Rails
26
+ default_options.merge(
27
+ :app => ::Rails.application,
28
+ #:env => ::Rails.env || DEFAULT_ENV,
29
+ #:groups => ["application"],
30
+ :app_filename => [File.join(::Rails.root, APP_SETTINGS_FILENAME), File.join(::Rails.root, "config", APP_SETTINGS_FILENAME), DEFAULT_SETTINGS_FILE],
31
+ :env_filename => [File.join(::Rails.root, ENV_VARS_FILENAME), File.join(::Rails.root, "config", ENV_VARS_FILENAME)]
32
+ )
33
+ else
34
+ default_options
35
+ end
36
+ end
37
+
38
+ attr_accessor :app, :env, :groups
39
+
40
+ def initialize(options)
41
+ @options = self.class.default_options.merge(options)
42
+ @app = @options[:app]
43
+ @env = @options[:env]
44
+ @groups = @options[:groups]
45
+ end
46
+
47
+ def configure(environment = nil, &block)
48
+ if block_given? && (environment.nil? || env == environment.to_s)
49
+ args = []
50
+ args << app if [-1, 1].include?(proc.arity)
51
+ args << env if [-1, 2].include?(proc.arity)
52
+
53
+ block.call(*args)
54
+ end
55
+ self
56
+ end
57
+
58
+ def puts_env(options = {})
59
+ puts(*list_env(options))
60
+ end
61
+
62
+ def list_env(options = {})
63
+ variables(options).collect{|key, value| "#{key}=#{value}"}
64
+ end
65
+
66
+ def save_env_file(environment, filename = nil)
67
+ filename ||= File.join(::Rails.root, "#{environment}.env")
68
+
69
+ File.open(filename, 'w') do |file|
70
+ list_env(:env => environment).each{|line| file.puts line }
71
+ end
72
+
73
+ filename
74
+ end
75
+
76
+ def variables(options = {})
77
+ options = @options.merge(options)
78
+ groups = Array.wrap(options[:groups])
79
+ grouped_variables = load_settings(@options[:env_filename])
80
+
81
+ groups.inject({}) do |all, group|
82
+ all.merge grouped_variables.fetch(options[:env], {}).fetch(group, {})
83
+ end
84
+ end
85
+
86
+ def settings(options = {})
87
+ options = @options.merge(options)
88
+ config = load_settings(options[:app_filename], options).fetch(env, {})
89
+ Settings.new(app, env, config)
90
+ end
91
+
92
+ protected
93
+
94
+ def load_settings(filename, options = {})
95
+ # allow multiple filenames and use the first one that exists
96
+ filename = Array.wrap(filename).find{|path| File.exists?(path)}
97
+
98
+ unless filename.nil?
99
+ source = File.read(filename)
100
+ source = ERB.new(source).result unless options[:skip_erb]
101
+ all = YAML.load(source)
102
+ defaults = all.delete('defaults') || {}
103
+
104
+ all.each do |environment, settings|
105
+ all[environment] = defaults.deep_merge(settings)
106
+ end
107
+ end
108
+
109
+ all || {}
110
+ end
111
+
112
+
113
+ end
114
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'rails/engine'
2
+ require_relative 'rails/version'
@@ -0,0 +1,106 @@
1
+ #require 'greased-rails'
2
+ require 'rails'
3
+
4
+ module Greased
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+
8
+ rake_tasks do
9
+ path = Pathname.new(File.join(File.dirname(__FILE__), '../../../tasks/')).realpath
10
+
11
+ Dir["#{path}/*.rake"].each do |filename|
12
+ load filename
13
+ end
14
+ end
15
+
16
+ #http://guides.rubyonrails.org/configuring.html
17
+ #before_configuration
18
+ #before_initialize - before initialization
19
+ #to_prepare - after railties and application initializers, before eager loading and middleware (on each request in dev)
20
+ #before_eager_load - default behavior in production
21
+ #after_initialize - after application initializes - BEFORE application initializers
22
+
23
+ #app.config.before_configuration
24
+ #:load_environment_hook
25
+ config.before_configuration do |application|
26
+
27
+ options = Applicator.rails_options.merge(:app => application)
28
+ applicator = Applicator.new(options)
29
+
30
+ applicator.variables(options).each do |key, value|
31
+
32
+ # Don't mess with the environment
33
+ next if ["RACK_ENV", "RAILS_ENV"].include?(key.to_s.upcase)
34
+
35
+ ENV[key.to_s] = value.to_s
36
+ end
37
+
38
+ puts ""
39
+ puts "############################## GREASED ##############################"
40
+ puts "# #"
41
+ puts "# ... loading application settings ... #"
42
+ puts "# #"
43
+ puts "#####################################################################"
44
+ puts ""
45
+
46
+ applicator.list_env.map.collect do |var|
47
+ puts " #{var}"
48
+ end
49
+
50
+ puts ""
51
+ puts "#####################################################################"
52
+ puts ""
53
+
54
+ end
55
+
56
+ # config.before_configuration {|app| puts "BEFORE CONFIGURATION"}
57
+ #
58
+ # config.before_initialize {|app| puts "BEFORE INITIALIZE"}
59
+ #
60
+ # config.to_prepare {|app| puts "TO PREPARE"}
61
+ #
62
+ # config.before_eager_load {|app| puts "BEFORE EAGER LOAD"}
63
+ #
64
+ # config.after_initialize {|app| puts "AFTER INITIALIZE"}
65
+ #
66
+ # hooks = [
67
+ # :load_environment_hook, :load_active_support, :preload_frameworks, :initialize_logger, :initialize_cache, :set_clear_dependencies_hook,
68
+ # :initialize_dependency_mechanism, :bootstrap_hook, "i18n.callbacks", :set_load_path, :set_autoload_paths, :add_routing_paths,
69
+ # :add_locales, :add_view_paths, :load_environment_config, :append_asset_paths, :prepend_helpers_path, :load_config_initializers,
70
+ # :engines_blank_point, :add_generator_templates, :ensure_autoload_once_paths_as_subset, :add_to_prepare_blocks, :add_builtin_route,
71
+ # :build_middleware_stack, :eager_load!, :finisher_hook, :set_routes_reloader, :disable_dependency_loading
72
+ # ]
73
+ #
74
+ # hooks.each do |hook_name|
75
+ # initializer("greased.before.#{hook_name}", :before => hook_name, :group => :all) {|a| puts "BEFORE #{hook_name}".upcase}
76
+ # end
77
+
78
+ # RUNS BEFORE ENVIRONMENT CONFIGS ARE LOADED!
79
+ #app.config.before_initialize
80
+ config.before_configuration do |application|
81
+
82
+ options = Applicator.rails_options.merge(:app => application)
83
+ applicator = Applicator.new(options)
84
+
85
+ applicator.settings.apply!
86
+
87
+ puts ""
88
+ puts "############################## GREASED ##############################"
89
+ puts "# #"
90
+ puts "# ... loading application settings ... #"
91
+ puts "# #"
92
+ puts "#####################################################################"
93
+ puts ""
94
+
95
+ applicator.settings.list.map(&:strip).map{|setting| setting.split("\n")}.flatten.each do |line|
96
+ puts " #{line}"
97
+ end
98
+
99
+ puts ""
100
+ puts "#####################################################################"
101
+ puts ""
102
+
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,5 @@
1
+ module Greased
2
+ module Rails
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+
2
+ module Greased
3
+ class Setting
4
+
5
+ DEFAULT_OPERATOR = "="
6
+ OPERATIONS = {
7
+ "<<" => lambda{ |target, name, value| target.send(:"#{name}<<", value) },
8
+ "+=" => lambda{ |target, name, value| target.send(:"#{name}=", target.send(:"#{name}") + value) },
9
+ "-=" => lambda{ |target, name, value| target.send(:"#{name}=", target.send(:"#{name}") - value) },
10
+ "*=" => lambda{ |target, name, value| target.send(:"#{name}=", target.send(:"#{name}") * value) },
11
+ "=" => lambda{ |target, name, value| target.send(:"#{name}=", value) },
12
+ "send" => lambda{ |target, name, value| target.send(name.to_sym, *value) }
13
+ }
14
+ OPERATION_VALIDATORS = {
15
+ "send" => lambda{|target, name, value| raise "Value for the 'send' operator must be an array of method parameters" unless value.is_a? Array }
16
+ }
17
+
18
+ attr_reader :name, :value, :operator, :app, :env
19
+
20
+ def initialize(app, target, name, value, env, parent_methods = [])
21
+ @name, @operator = name.split(/\s+/, 2)
22
+ @value = value
23
+ @operator ||= DEFAULT_OPERATOR
24
+ @target = target
25
+ @app = app
26
+ @env = env
27
+ @parent_methods = parent_methods
28
+ end
29
+
30
+ def serialize
31
+ #"#{(@parent_methods + [name]).join('.')} #{operator} #{PP.pp(value, '').sub(/^([\[\{])/, "\1\n").sub(/([\]\}])$/, "\n\1")})}"
32
+ "#{(@parent_methods + [name]).join('.')} #{operator} #{PP.pp(value, '').sub(/^([\[\{])/, "\\1\n ").sub(/([\]\}])$/, "\n\\1")}"
33
+ end
34
+
35
+ def apply!
36
+ raise "Unknown operator (#{operator}) for setting option: #{name}" unless OPERATIONS.keys.include? operator
37
+ OPERATION_VALIDATORS[operator].call(@target, name, value) if OPERATION_VALIDATORS.keys.include? operator
38
+ OPERATIONS[operator].call(@target, name, value)
39
+
40
+ self
41
+ end
42
+
43
+ class << self
44
+
45
+ def from_config(app, property_name, property_values, env)
46
+ app_target = app.send(property_name.to_sym)
47
+
48
+ property_values.collect do |setting_name, setting_values|
49
+ parents = Array.wrap(property_name)
50
+
51
+ if (target = app_target.send(setting_name.to_sym)).is_a? ActiveSupport::OrderedOptions
52
+ parents << setting_name
53
+ else
54
+ setting_values = { setting_name => setting_values }
55
+ target = app_target
56
+ end
57
+
58
+ setting_values.collect{ |key, value| new(app, target, key, value, env, parents) }
59
+ end.flatten
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,43 @@
1
+
2
+ module Greased
3
+ class Settings
4
+ include Enumerable
5
+
6
+ def initialize(application, environment, config = {})
7
+ @application = application
8
+ @environment = environment
9
+ @config = config
10
+ @settings = []
11
+
12
+ refresh_settings!
13
+ end
14
+
15
+ def serialize
16
+ @settings.collect(&:serialize).join("\n")
17
+ end
18
+
19
+ def apply!
20
+ @settings.each(&:apply!)
21
+ self
22
+ end
23
+
24
+ def puts!
25
+ puts(*list)
26
+ end
27
+
28
+ def list
29
+ @settings.collect(&:serialize)
30
+ end
31
+
32
+ def each(&block)
33
+ @settings.each(&block)
34
+ self
35
+ end
36
+
37
+ protected
38
+
39
+ def refresh_settings!
40
+ @settings = @config.collect{ |key, value| Setting.from_config(@application, key, value, @environment) }.flatten
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ require 'greased-rails'
2
+
3
+ namespace :greased do
4
+ namespace :env do
5
+ task :dump => :environment do
6
+ options = Greased::Applicator.rails_options
7
+ settings = Greased::Applicator.new(options)
8
+
9
+ puts ""
10
+ puts "############################## GREASED ##############################"
11
+ puts "# #"
12
+ puts "# ... dumping variables to *.env files ... #"
13
+ puts "# #"
14
+ puts "#####################################################################"
15
+ puts ""
16
+
17
+ environments = ["development", "staging", "production"]
18
+ longest = environments.map(&:size).max
19
+
20
+ environments.each do |env|
21
+ path = settings.save_env_file(env)
22
+ filename = Pathname.new(path).basename
23
+
24
+ puts " [#{env}]#{' ' * (longest - env.size)} filename: #{path}"
25
+ end
26
+
27
+ puts ""
28
+ puts "#####################################################################"
29
+ puts ""
30
+
31
+ end
32
+ end
33
+
34
+ namespace :heroku do
35
+ task :deploy, [:env] => [:environment] do |t, args|
36
+
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: greased-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joel Van Horn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: railties
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0
46
+ description: Reusable default application settings, environment variables, and deployment
47
+ tasks.
48
+ email:
49
+ - joel@joelvanhorn.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - examples/greased_settings.yml
60
+ - examples/greased_variables.yml
61
+ - greased-rails.gemspec
62
+ - lib/greased-rails.rb
63
+ - lib/greased/applicator.rb
64
+ - lib/greased/rails.rb
65
+ - lib/greased/rails/engine.rb
66
+ - lib/greased/rails/version.rb
67
+ - lib/greased/setting.rb
68
+ - lib/greased/settings.rb
69
+ - tasks/greased.rake
70
+ homepage: http://rubygems.org/gems/greased-rails
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ - tasks
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.25
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Replicate common Rails application settings and environment variables using
95
+ templates. Handy deployment tasks make managing your environments easier.
96
+ test_files: []