Skip to content

Commit

Permalink
Refactor config/ directory
Browse files Browse the repository at this point in the history
* Get rid of early pathname requirement
* Use require_relative where approciate
* Drop unused files
* Imported new application.rb, environment.rb and environments/* from fresh Rails app
* Cleaned up boot.rb
* Load config in boot.rb
* Deduplicate environments/integration*.rb
* Move username blacklist into defaults.yml
* Ruby 1.9 Hash syntax everywhere
* Reorganize lib/diaspora/markdownify to match conventions
* Get rid of full path requires where possible
* Add dummy content to production section diaspora.yml.example to prevent warning
* Drop sqlite? method
* Move postgres? method into ConfigurationMethods
* Drop token authentication from User
  • Loading branch information
jhass committed May 19, 2013
1 parent bf90dff commit ac147cc
Show file tree
Hide file tree
Showing 40 changed files with 257 additions and 446 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## Refactor

* Refactored config/ directory [#4145](https://github.com/diaspora/diaspora/pull/4145).

## Bug fixes

* Don't use Pathname early to circumvent some rare initialization errors [#3816](https://github.com/diaspora/diaspora/issues/3816)

## Features


Expand Down
4 changes: 2 additions & 2 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def to_param

def self.search_query_string(query)
query = query.downcase
like_operator = postgres? ? "ILIKE" : "LIKE"
like_operator = AppConfig.postgres? ? "ILIKE" : "LIKE"

where_clause = <<-SQL
profiles.full_name #{like_operator} ? OR
Expand Down Expand Up @@ -155,7 +155,7 @@ def self.search(query, user)
# @return [Array<String>] postgreSQL and mysql deal with null values in orders differently, it seems.
def self.search_order
@search_order ||= Proc.new {
order = if postgres?
order = if AppConfig.postgres?
"ASC"
else
"DESC"
Expand Down
2 changes: 1 addition & 1 deletion app/models/share_visibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ShareVisibility < ActiveRecord::Base
def self.batch_import(contact_ids, share)
return false unless ShareVisibility.new(:shareable_id => share.id, :shareable_type => share.class.to_s).valid?

if postgres?
if AppConfig.postgres?
contact_ids.each do |contact_id|
ShareVisibility.find_or_create_by_contact_id_and_shareable_id_and_shareable_type(contact_id, share.id, share.class.base_class.to_s)
end
Expand Down
5 changes: 2 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :lockable, :lock_strategy => :none,
:unlock_strategy => :none
:lockable, :lock_strategy => :none, :unlock_strategy => :none

before_validation :strip_and_downcase_username
before_validation :set_current_language, :on => :create

validates :username, :presence => true, :uniqueness => true
validates_format_of :username, :with => /\A[A-Za-z0-9_]+\z/
validates_length_of :username, :maximum => 32
validates_exclusion_of :username, :in => USERNAME_BLACKLIST
validates_exclusion_of :username, :in => AppConfig.settings.username_blacklist
validates_inclusion_of :language, :in => AVAILABLE_LANGUAGE_CODES
validates_format_of :unconfirmed_email, :with => Devise.email_regexp, :allow_blank => true

Expand Down
112 changes: 60 additions & 52 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.

require 'pathname'
require Pathname.new(__FILE__).expand_path.dirname.join('boot')

require 'yaml'
require_relative 'boot'

require 'rails/all'

# Sanitize groups to make matching :assets easier
RAILS_GROUPS = Rails.groups(:assets => %w(development test)).map { |group| group.to_sym }

if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*RAILS_GROUPS)
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
Bundler.require(*Rails.groups(:assets => %w(development test))) if defined?(Bundler)

module Diaspora
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Add additional load paths for your own custom dirs
config.autoload_paths += %W{#{config.root}/app/presenters #{config.root}/app}
config.autoload_once_paths += %W{#{config.root}/lib}
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W{#{config.root}/app}
config.autoload_once_paths += %W{#{config.root}/lib}

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Activate observers that should always be running
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
Expand All @@ -44,47 +28,71 @@ class Application < Rails::Application
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de

# Configure generators values. Many other options are available, be sure to check the documentation.
config.generators do |g|
g.template_engine :haml
g.test_framework :rspec
end

# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"

# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
config.filter_parameters += [:xml]
config.filter_parameters += [:message]
config.filter_parameters += [:text]
config.filter_parameters += [:bio]
config.filter_parameters += [:password, :xml,:message, :text, :bio]

# Enable escaping HTML in JSON.
config.active_support.escape_html_entities_in_json = true

# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql

# Enforce whitelist mode for mass assignment.
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
#config.active_record.whitelist_attributes = true

# Enable the asset pipeline
config.assets.enabled = true

config.assets.initialize_on_precompile = false

# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# Javascripts
config.assets.precompile += [ "aspect-contacts.js", "contact-list.js", "finder.js",
"home.js", "ie.js", "inbox.js", "jquery.js", "jquery_ujs.js", "jquery.textchange.js",
"login.js", "mailchimp.js", "main.js", "mobile.js", "profile.js", "people.js", "photos.js",
"profile.js", "publisher.js", "templates.js", "validation.js" ]

# Stylesheets
config.assets.precompile += [ "blueprint.css", "bootstrap.css", "bootstrap-complete.css",
"bootstrap-responsive.css", "default.css", "error_pages.css", "login.css", "mobile.css",
"new-templates.css", "rtl.css" ]

# Rails Admin - these assets need to be added here since the Engine initializer
# doesn't run with initialize_on_precompile disabled. This list is taken
# directly from the Rails Admin Engine initializer.
config.assets.precompile += ['rails_admin/rails_admin.js', 'rails_admin/rails_admin.css',
'rails_admin/jquery.colorpicker.js', 'rails_admin/jquery.colorpicker.css']
config.assets.precompile += %w{
aspect-contacts.js
contact-list.js
finder.js
home.js
ie.js
inbox.js
jquery.js
jquery_ujs.js
jquery.textchange.js
login.js
mailchimp.js
main.js
mobile.js
profile.js
people.js
photos.js
profile.js
publisher.js
templates.js
validation.js

blueprint.css
bootstrap.css
bootstrap-complete.css
bootstrap-responsive.css
default.css
error_pages.css
login.css
mobile.css
new-templates.css
rtl.css
}

# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'

# Configure generators values. Many other options are available, be sure to check the documentation.
config.generators do |g|
g.template_engine :haml
g.test_framework :rspec
end
end
end
24 changes: 9 additions & 15 deletions config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.

require 'pathname'
require 'rubygems'

# Set up gems listed in the Gemfile.
gemfile = Pathname.new(__FILE__).dirname.join('..').expand_path.join('Gemfile')
begin
ENV['BUNDLE_GEMFILE'] = gemfile.to_s
require 'bundler'
Bundler.setup
rescue Bundler::GemNotFound => e
STDERR.puts e.message
STDERR.puts "Try running `bundle install`."
exit!
end if File.exist?(gemfile)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

# Ensure Builder is loaded
require 'active_support/builder' unless defined?(Builder)

# Load configuration early
require_relative 'load_config'
25 changes: 20 additions & 5 deletions config/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defaults:
concurrency: 5
retry: 10
backtrace: 15
log: "log/sidekiq.log"
log: 'log/sidekiq.log'
s3:
enable: false
key:
Expand Down Expand Up @@ -50,7 +50,7 @@ defaults:
mixpanel_uid:
chartbeat_uid:
settings:
pod_name: "Diaspora*"
pod_name: 'Diaspora*'
enable_registrations: true
follow_diasporahq: true
invitations:
Expand All @@ -61,6 +61,21 @@ defaults:
enable: false
suggest_email:
typhoeus_verbose: false
username_blacklist:
- 'admin'
- 'administrator'
- 'hostmaster'
- 'info'
- 'postmaster'
- 'root'
- 'ssladmin'
- 'ssladministrator'
- 'sslwebmaster'
- 'sysadmin'
- 'webmaster'
- 'support'
- 'contact'
- 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo'
services:
facebook:
enable: false
Expand Down Expand Up @@ -109,7 +124,7 @@ production:
i_am_a_dummy: # Remove if you add an actual override
test:
environment:
url: "http://localhost:9887/"
url: 'http://localhost:9887/'
single_process_mode: true
require_ssl: false
assets:
Expand All @@ -127,14 +142,14 @@ test:
enable: true
integration1:
environment:
url: "http://localhost:45789/"
url: 'http://localhost:45789/'
single_process_mode: true
assets:
serve: true
require_ssl: false
integration2:
environment:
url: "http://localhost:34658/"
url: 'http://localhost:34658/'
single_process_mode: true
assets:
serve: true
Expand Down
2 changes: 2 additions & 0 deletions config/diaspora.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ configuration: ## Section
## Here you can make overides to settings defined above if you need
## to have them different in different environments.
production: ## Section
environment: ## Section
#redis_url: 'redis://production.example.org:6379'

development: ## Section
environment: ## Section
Expand Down
41 changes: 1 addition & 40 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.

# check what database you have
def postgres?
@using_postgres ||= defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
end

def sqlite?
@using_sqlite ||= defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter) && ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter
end

# Load the rails application
require Pathname.new(__FILE__).dirname.expand_path.join('application')

# Load configuration system early
require Rails.root.join('config', 'load_config')

Haml::Template.options[:format] = :html5
Haml::Template.options[:escape_html] = true

# Blacklist of usernames
USERNAME_BLACKLIST = ['admin', 'administrator', 'hostmaster', 'info', 'postmaster', 'root', 'ssladmin',
'ssladministrator', 'sslwebmaster', 'sysadmin', 'webmaster', 'support', 'contact', 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo']
require_relative 'application'

# Initialize the rails application
Diaspora::Application.initialize!

# allow token auth only for posting activitystream photos
module Devise
module Strategies
class TokenAuthenticatable < Authenticatable
private
def valid_params_request?
params[:controller] == "activity_streams/photos" && params[:action] == "create"
end
end
end
end


# Ensure Builder is loaded
require 'active_support/builder' unless defined?(Builder)
Loading

0 comments on commit ac147cc

Please sign in to comment.