This gem contains some developer quality of life scripts and rails helpers.
To use the included rails helpers and rake tasks in your current rails project, add to the Gemfile:
group :development
gem 'effective_developer'
endRun the bundle command to install it:
bundle installTo use the included command line shell scripts in any directory, clone this repo:
git clone [email protected]:code-and-effect/effective_developer.gitand add the following to your PATH (edit your ~/.bashrc or ~/.profile):
export PATH="$PATH:$HOME/effective_developer/bin"To use the included git hooks for all git repos, run the following:
git config --global core.hooksPath ~/effective_developer/githooks
To use the included command line shell scripts:
export PATH="$PATH:$HOME/effective_developer/bin"A command line shell script to update a Gemfile and use any provided gems locally.
This makes it very quick to swich to developing a gem locally.
gem_develop should be run from the root directory of any rails app.
> gem_develop effective_datatables effective_resourcesto change:
gem 'effective_datatables'
gem 'effective_resources'into:
gem 'effective_datatables', path: '~/Sites/effective_datatables'
gem 'effective_resources', path: '~/Sites/effective_resources'and execute bundle.
You can override the ~/Sites/ directory by setting ENV['GEM_DEVELOP_PATH'].
A command line shell script that quickly bumps the version of any ruby gem.
It checks for any uncommitted files, updates the gem's version.rb with the given version, makes a single file git commit with a tag and message, then runs git push origin master, gem build and gem push to rubygems.
gem_release should be run from the root directory of any ruby gem.
To print the current gem version:
> gem_releaseTo release a new gem version:
> gem_release 1.0.0A command line shell script to update a Gemfile to find any locally developed gems, and update them to the most current released version.
gem_reset should be run from the root directory of any rails app.
Just run with no arguments:
> gem_resetto change:
gem 'effective_datatables', path: '~/Sites/effective_datatables'
gem 'effective_resources', path: '~/Sites/effective_resources'into:
gem 'effective_datatables'
gem 'effective_resources'and execute bundle update effective_datatables effective_resources.
Careful, this command will delete all your un committed changes.
A command line script to call git reset --hard and also delete any newly created files.
It truly resets you back to a fresh working copy. Perfect for tweaking scaffold and code generation tools.
> gitresetA command line script to delete any git branch that has already been merged into master & develop
> gitsweepkill -9 the first running puma process. Bails out of SystemStackError (stack level too deep).
> killpumaTo use the included git hooks for all git repos, run the following:
git config --global core.hooksPath ~/effective_developer/githooks
Prevents pushing git commits that have the following bad patterns:
- Gemfile includes 'path: ' gems
- binding.pry
Exports all database tables to individual .csv files.
rake export:csvWhere table is the name of a model. Dynamically created rake task when a /lib/csv_importers/foos.rb file is present.
rake csv:import:foosScaffolds an Effective::CsvImporter file for each .csv file in /lib/csv_importers/data/*.csv
rake csv:scaffoldor
rake csv:scaffold[users]Quickly rename a rails class throughout the entire app.
The script considers the .classify, .pluralize and .singularize common usage patterns.
Then performs a global search and replace, and renames files and directories.
rake rename_class[account,team]or
rake rename_class[account,team,skipdb]to skip any changes to database migrations.
If you ever run into the error duplicate key violates unique constraint (id) error, run this script:
rake reset_pk_sequenceThis makes sure that the autoincremented (postgresql) Pk sequence matches the correct id value.
Creates a new backup on heroku, downloads that backup to latest.dump, and then calls pg:load
rake pg:pull
rake pg:pull[staging]By default, any data in the logs table will be excluded. To include the logs data:
rake pg:pull logs=trueDrops and re-creates the local database then initializes database with the contents of latest.dump
rake pg:load
rake pg:load[something.dump]Saves the development database to a postgresql .dump file (latest.dump by default)
rake pg:save
rake pg:save[something.dump]Clones the production (--remote heroku by default) database to staging (--remote staging by default)
rake pg:clone
rake pg:clone[origin,staging]Loads every ActiveRecord object and calls .valid? on it.
rake validate
rake validate[post]Extend a class from Effective::CsvImporter to quickly build a csv importer.
Put your importer in lib/csv_importers/users_importer.rb and the data in lib/csv_importers/data/users.csv. Both filenames should be pluralized.
A rake command will be dynamically created rake import:users.
The importer requires two instance methods be defined:
def columnsa hash of names to columns. The constantsA== 0,B== 1, uptoAT== 45 are defined to work better with spreadsheets.def process_rowwill be run for each row of data. Any exceptions will be caught and logged as errors.
Inside the script, there are a few helpful functions:
col(:email)will return the normalized value in that column.last_row_col(:email)will return the normalized value for this column in the previous row.raw_col(:email)will return the raw value in that column
module CsvImporters
class UsersImporter < Effective::CsvImporter
def columns
{
email: A,
first_name: B,
last_name: C,
admin?: D
}
end
def process_row
User.new(email: col(:email), first_name: col(:first_name), last_name: col(:last_name), admin: col(:admin?)).save!
end
end
endWhen using col() or last_row_col() helpers, the value is normalized.
- Any column that ends with a
?will be converted into a boolean. - Any column that ends with
_atwill be converted into a DateTime. - Any column that ends with
_onwill be converted into a Date. - Override
def normalizeto add your own.
def normalize(column, value)
if column == :first_name
value.upcase
else
super
end
endOverride before_import() or after_import() to run code before or after the import.
The goal of the effective_developer code generation project is to minimize the amount of hand coding required to build a rails website.
Only the rails model file should be written by a human.
All database migrations, controllers, forms and views should be generated.
Creating a new working in-place CRUD feature should be a 1-liner.
A huge head start to the interesting part of the code.
Scaffolding is the fastest way to build a CRUD rails app.
The effective scaffolds generally follow the same pattern as the rails generate commands.
To create an entire CRUD resource from the command line:
rails generate effective:scaffold thing name:string description:text
rails generate effective:scaffold thing name:string description:text --actions index show mark_as_paid
rails generate effective:scaffold admin/thing name:string description:text
rails generate effective:scaffold admin/thing name:string description:text --actions crud-showOr to skip the model & migration:
rails generate effective:scaffold_controller thing
rails generate effective:scaffold_controller thing index show
rails generate effective:scaffold_controller thing index show --attributes name description
rails generate effective:scaffold_controller admin/thing crud mark_as_paid
rails generate effective:scaffold_controller admin/thing crud-showIf there is a regular rails model file present, all attributes, belong_tos, scopes and has_many accepts_nested_attributes will be considered when generating the scaffold.
Make a model file like this (or generate it with rails generate effective:model post name:string body:text and tweak from there):
class Post < ApplicationRecord
belongs_to :user
belongs_to :category
# Attributes
effective_resources do
title :string
body :text
published_at :datetime
end
validates :title, presence: true
validates :description, presence: true
has_many :comments
accepts_nested_attributes_for :comments
scope :published, -> { where.not(published_at: nil) }
def to_s
title || 'New Post'
end
endand then run
rails generate effective:scaffold post
rails generate effective:scaffold_controller admin/postTweak from here
You can call scaffolds one at a time:
# These two accept attributes on the command line. like effective:scaffold
rails generate effective:model thing name:string description:text
rails generate effective:migration thing name:string description:text
# Thes accept actions on the command line. Pass --attributes as an option. like effective:scaffold_controller
rails generate effective:controller thing # /admin/thing
rails generate effective:route thing
rails generate effective:ability thing # CanCanCan
rails generate effective:menu thing # If app/views/*namespaces/_navbar.html.haml is present
rails generate effective:datatable thing
rails generate effective:views thing
rails generate effective:form thingYou can do this:
Region.update_all("snippets = REPLACE(snippets, 'ActionController::Parameters', 'ActiveSupport::HashWithIndifferentAccess')")
To find & replace content in the action_text_rich_texts body column:
Effective::ContentReplacer.new.count("foo")
and
Effective::ContentReplacer.new.replace!("foo", "bar")
MIT License. Copyright Code and Effect Inc.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Bonus points for test coverage
- Create new Pull Request