Git Product home page Git Product logo

console_update's Introduction

Description

Updates records from the console via your preferred editor. You can update a record’s columns as well as any attribute that has accessor methods. Records are edited via a temporary file and once saved, the records are updated. Records go through a filter before and after editing the file. Yaml is the default filter, but you can define your own filter simply with a module and 2 expected methods. See ConsoleUpdate::Filter for more details. Compatible with all major Ruby versions and Rails 2.3.x and up.

Install

Install as a gem

$ gem install console_update

# with bundler: add in Gemfile
gem 'console_update'

# without bundler: add in config/environment.rb
config.gem "console_update"

Or as a plugin

$ script/plugin install git://github.com/cldwalker/console_update.git

Examples

For a given model Url, update your records as you please:

$ script/console

# Update a record from the object
>> Url.first.console_update

# Update a group of records
>> records = Url.all :limit=>10
>> Url.console_update records

# Find and update by a given id
>> Url.find_and_console_update 10

# Update through any named_scope ie tagged_with()
>> Url.tagged_with("sweetness").console_update

Setup

Define your editor if not already picked up by environment variable $EDITOR:

ConsoleUpdate.editor = 'vim'

Configure model(s) to update from the console:

class Url
  can_console_update
end

By default, can_console_update() has sensical defaults for what attributes to update. But you can setup your own defaults as needed:

can_console_update :only=>%w{column1 column2 relation_accessor1}
can_console_update :except=>%w{column2}

To use the named_scope chaining, enable it once.

ConsoleUpdate.enable_named_scope

More Examples

Although console_update() uses the default editable columns, it can take options to override these as needed. Note these options can be passed to any of the console_update-like methods shown above:

records = Url.all :limit=>100
# Only edit this one attribute
Url.console_update records, :only=>%w{description}
# Edit all the default attributes except this one
Url.console_update records, :except=>%w{description}

As mentioned above, any attribute can be edited. This means it’s possible to edit associated values as well as column values.

Say we have a Url that has many tags and accessor methods to edit them ie tag_list() and tag_list=():

@url.tag_list = ['tag1', 'tag2']
@url.save
@url.tag_list # =>['tag1', 'tag2']

By simply passing ‘tag_list’ as another attribute to console_update() or can_console_update(), we can edit these associated values:

class Url
  can_console_update :only=>%w{column1 column2 tag_list}
end

Url.console_update records, :only=>%w{column1 column2 tag_list}

Caveats

So should you be updating production records with this plugin? Yes and no. Yes, if you’re updating some simple string/text values. If editing more complex objects ie non-string objects and associated objects, try edge cases to ensure the updates work as expected. Although this plugin already comes with decent tests, I’m always open to patches for edge cases I may have missed.

Motivation

The need for editing speed in my console-based project.

Bugs/Issues

Please report them on github.

Todo

  • Have a config file as an alternative configuration method which doesn’t clutter models with can_console_update() calls.

  • Make ORM-agnostic.

console_update's People

Contributors

cldwalker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

codesnik

console_update's Issues

no implicit conversion of nil into String

Hi Gabriel, big fan of your work, thank you for all your contributions.

I was using console_update, hirb, and boson heavily in an inventory management application in 2013. I've tried using console update in my new projects but am getting this error.

Some record(s) didn't update because of this error: no implicit conversion of nil into String

Even when scoping like such: can_console_update :only => %w{name}

I've tried calling console update on single records i.e. Product.first.console_update and multiple records but get the error no matter what.

I am using:
ruby '2.6.2'
gem 'rails', '~> 5.2.2', '>= 5.2.2.1'

Sorry I'm not smart enough to debug this on my own, I would appreciate you looking into it. BTW thanks for the alias gem, saves me so many keystrokes everyday.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.