Code Monkey home page Code Monkey logo

lightrails's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar ryohashimoto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lightrails's Issues

Test helpers

Provide test helper module (methods) for facade / interactor / representer

retrieve_facade method

class ArchivesController < ApplicationController

def day
  # guess facade automatically
  retrieve_facade :year, :month, :day, :pager, :posts
  # instead of below:
  # facade = Archives::DayFacade.new(params)
  # retrieve facade, :year, :month, :day, :pager, :posts
end

Generator for lightrails:install

Instead of the example below, it's useful if we can run bin/rails generate lightrails:install command.

$ bin/rails generate action_facade:install
$ bin/rails generate action_interactor:install
$ bin/rails generate active_representer:install

Result item shortcut

It would be useful if result[:item_name] can be accessed by result.item_name.

class RegistrationInteractor < ActionInteractor::Base
  attr_result :user # user is given after execution
  ...

registration = RegistrationInteractor.execute(name: 'John')
user = interactor.user # same as interactor.result[:user]

Rake tasks for Rails project

Provide rake (rails) tasks for Rails project.

For ActiveRepresenter

$ rails active_representer:install
create app/representers
create app/representers/application_representer.rb

For ActionInteractor

$ rails action_interactor:install
create app/interactors
create app/interactors/application_interactor.rb

Set controller/view instance variables from facade

class Mypage::IndexFacade < ApplicationFacade
  attr_reader :current_user

  def initialize(params)
    @current_user = params[:current_user]
  end

  def active_users
    @active_users ||= User.active.order(login_at: :desc).limit(10)
  end

  def messages
    @messages ||= current_user.messages.order(created_at: :desc).limit(10)
  end
end

# in MypageController
def index
  facade = Mypage::IndexFacade.new(current_user: current_user)
  # instead of:
  # @active_users = facade.active_users
  # @messages = facade.messages
  retrieve facade, :active_users, :messages
end

use ActiveModel::Attributes

class ActiveRepresenter::Base
  include ActiveModel::Model
  include ActiveModel::Attributes
end

class Song  < ActiveRepresenter::Base
  attribute :title, :string
  attribute :published, :boolean, default: true
end

Helper alternative (not global / namespaced)

It can be used like helper, but only valid in the namespace.

module MypageBacker # backer or another good name
  extend ActionBacker::Base

  def method_a
    ...
  end
end
  • ActionBacker::Base module is like ActiveSupport::Concerns
  • The method_a can be used for templates in /app/views/mypage but it can't be used for templates in other directories.
module ApplicationBacker
  extend ActionBacker::Base

  include MypageBacker
end
  • The method_a can be used in all templates in the app.

JSON rendering

Like roar, representer can render JSON for containing object.

song = Song.new(title: "Medicine Balls")
SongRepresenter.new(song).to_json #=> {"title":"Medicine Balls"}

Memoize method

If memonize :method_name is added, automatically add @method_name ||= to each method.

class Mypage::IndexFacade < ApplicationFacade
  attr_reader :current_user
  memoize :active_user, :messages

  def initialize(params)
    @current_user = params[:current_user]
  end

  def active_user
    User.active.order(login_at: :desc).limit(10)
    # same as below if memonize :active_user is set
    # @active_user ||= User.active.order(login_at: :desc).limit(10)
  end

  def messages
    current_user.messages.order(created_at: :desc).limit(10)
    # same as below if memonize :messages is set
    # @messages ||= current_user.messages.order(created_at: :desc).limit(10)
   end
end

Set all results at once in interactor

use results method like this.

class RegistrationInteractor < ApplicationInteractor
  def execute
    return fail! unless params[:name]
    results(
      user: User.new(name: params[:name]),
      activities: [Activity.new(title: 'Registration completed.']
    )
    success!
  end
end

Nested Representer

class AlbumRepresenter < ActiveRepresenter::Base
  attr_collection :songs, representer: SongRepresenter
end
album = Album.new(title: "Scary Monsters", songs: [{title: "It's No Game"}, {title: "Up the Hill Backwards"}])
representer = AlbumRepresenter.new(album)
representer.songs.first.class # => SongRepresenter (automatically converted to a representer)

Dependabot can't resolve your Ruby dependency files

Dependabot can't resolve your Ruby dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Bundler::VersionConflict with message: Bundler found conflicting requirements for the Ruby version:
  In Gemfile:
    Ruby (~> 2.4.10.0)

    lightrails was resolved to 0.2.5, which depends on
      railties (>= 6.1.3.1, <= 6.1.3.2) was resolved to 6.1.3.2, which depends on
        Ruby (>= 2.5.0)

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Composite Interactors

Add ability to compose sub interactors in a parent interactor.

leader_interactor = LeaderInteractor.new
buyer_interactor = BuyerInteractor.new
cook_interactor = CookInteractor.new
arranger_interactor = ArrangerInteractor.new

leader_interactor.add_interactor(buyer_interactor)
leader_interactor.add_interactor(cook_interactor)
leader_interactor.add_interactor(arranger_interactor)
leader_interactor.execute
leader_interactor.succcessful? # => true

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.