• Stars
    star
    1,116
  • Rank 41,621 (Top 0.9 %)
  • Language
    Elixir
  • License
    Other
  • Created over 8 years ago
  • Updated 11 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A rapid admin generator for Elixir & Phoenix

License Hex.pm Build Status Coverage Status

Torch

This version of Torch (5.x) only supports Phoenix 1.7 and above and is not fully backwards-compatible with previous versions of Torch. See UPGRADING for more details.

See v4.0 if you need support for Phoenix 1.6

See v3.0 if you need support for Phoenix 1.5 and below

Torch is a rapid admin generator for Phoenix applications. It creates custom templates and relies on the Phoenix HTML generator under the hood.

image

Requirements

Upgrading

If you are upgrading from Torch v4 (or earlier) you can find additional documentation in the UPGRADING file.

Installation

To install Torch, perform the following steps:

  1. Add torch to your list of dependencies in mix.exs. Then, run mix deps.get:
def deps do
  [
    {:torch, "~> 5.1"}
  ]
end
  1. Add a Plug.Static plug to your endpoint.ex:
plug(
  Plug.Static,
  at: "/torch",
  from: {:torch, "priv/static"},
  gzip: true,
  cache_control_for_etags: "public, max-age=86400",
  headers: [{"access-control-allow-origin", "*"}]
)
  1. Configure Torch by adding the following to your config.exs.
config :torch,
  otp_app: :my_app_name
  1. Run mix torch.install

Now you're ready to start generating your admin! 🎉

Usage

Torch uses Phoenix generators under the hood. Torch injects it's own custom templates into your priv/static directory, then runs the mix phx.gen.html task with the options you passed in. Finally, it uninstalls the custom templates so they don't interfere with running the plain Phoenix generators.

In light of that fact, the torch.gen.html task takes all the same arguments as the phx.gen.html, but does some extra configuration on either end. Checkout mix help phx.gen.html for more details about the supported options and format.

For example, if we wanted to generate a blog with a Post model we could run the following command:

# mix torch.gen.html <Context Module> <Schema Module> <Schema Table Name> [<Column Name>:<Column Type>]+
$ mix torch.gen.html Blog Post posts title:string body:text published_at:datetime published:boolean views:integer

The output would look like:

* creating priv/templates/phx.gen.html/edit.html.heex
* creating priv/templates/phx.gen.html/form.html.heex
...<omitted for brevity>...
* injecting test/phx1_6/blog_test.exs
* injecting test/support/fixtures/blog_fixtures.ex

Add the resource to your browser scope in lib/phx1_6_web/router.ex:

    resources "/posts", PostController


Remember to update your repository by running migrations:

    $ mix ecto.migrate

Ensure the following is added to your endpoint.ex:

    plug(
      Plug.Static,
      at: "/torch",
      from: {:torch, "priv/static"},
      gzip: true,
      cache_control_for_etags: "public, max-age=86400",
      headers: [{"access-control-allow-origin", "*"}]
    )

Also don't forget to add a link to layouts/torch.html if desired.

    <nav class="torch-nav">
      <!-- nav links here -->
    </nav>

Torch also installed an admin layout into your my_app_web/templates/layout/torch.html.heex. You will want to update it to include your new navigation link:

<nav class="torch-nav">
  <a href="/posts">Posts</a>
</nav>

There may be times when you are adding Torch into an already existing system where your application already contains the modules and controllers and you just want to use the Torch admin interface. Since the torch.gen mix tasks are just wrappers around the existing phx.gen tasks, you can use most of the same flags. To add an admin interface for Posts in the previous example, where the model and controller modules already exist, use the following command:

$ mix torch.gen.html Blog Post posts --no-schema --no-context --web Admin title:string body:text published_at:datetime published:boolean views:integer

Torch.Pagination customization

The following assumes you the above example when running torch.gen.html.

By default, the Torch generators added the following code to your Blog context module:

# blog.ex

  use Torch.Pagination,
    repo: MyApp.Repo,
    model: MyApp.Blog.Post,
    name: :posts

Please refer to the Torch.Pagination module for documentation on how to customize the pagination options for each model, or globally for your whole application.

NOTE If you want to customize the pagination functions themselves for your application, do not use the default Torch.Pagination as described above; instead you will need to define your own paginate_*/2 method that will return a Scrivener.Page object. You can also define your own pagination system and functions as well, but that will require further customization of the generated Torch controllers as well.

Association filters

Torch does not support association filters at this time. Filtrex does not yet support them.

You can checkout these two issues to see the latest updates:

rcdilorenzo/filtrex#55

rcdilorenzo/filtrex#38

However, that does not mean you can't roll your own.

Example

We have a Accounts.User model that has_many :credentials, Accounts.Credential and we want to support filtering users by credentials.email.

  1. Update the Accounts domain.
# accounts.ex
...
defp do_paginate_users(filter, params) do
  credential_params = Map.get(params, "credentials")
  params = Map.drop(params, ["credentials"])

  User
  |> Filtrex.query(filter)
  |> credential_filters(credential_params)
  |> order_by(^sort(params))
  |> paginate(Repo, params, @pagination)
end

defp credential_filters(query, nil), do: query

defp credential_filters(query, params) do
  search_string = "%#{params["email"]}%"

  from(u in query,
    join: c in assoc(u, :credentials),
    where: like(c.email, ^search_string),
    group_by: u.id
  )
end
...
  1. Update form filters.
# users/index.html.heex
<div class="field">
  <label>Credential email</label>
  <%= text_input(:credentials, :email, value: maybe(@conn.params, ["credentials", "email"])) %>
</div>

Note: You'll need to install & import Maybe into your views {:maybe, "~> 1.0.0"} for the above heex to work.

Styling

Torch generates two CSS themes you can use: base.css & theme.css. The base styles are basically bare bones, and the theme styles look like the screenshot above. Just change the stylesheet link in the torch.html.heex layout.

If you want to use the theme, but override the colors, you'll need to include your own stylesheet with the specific overrides.

Internationalization

Torch comes with .po files for several locales. If you are using Torch and can provide us with translation files for other languages, please submit a Pull Request with the translation file. We'd love to add as many translations as possible.

If you wish to add your own customized translations, you can configure Torch to use your own custom MessagesBackend and adding it in your Torch configuration settings in config.exs. You can find the all messages that can be customized in the default i18n/backend.ex file.

If you are customizing a backend for a "standard" spoken language, please submit back a proper .po translation file for us to include in the official Torch releases so other users can take advantage.

Example

defmodule MyApp.CustomMessagesBackend do
  def message("Contains"), do: "** CUSTOM Contains **"
  def message("Equals"), do: "** CUSTOM Equals ****"
  def message("< Prev"), do: "<--"
  def message("Next >"), do: "-->"

  # You can add a fallback so it won't break with newly added messages or
  # messages you did not customize
  def message(text), do: Torch.I18n.Backend.message(text)
end
# config.exs
config :torch,
  otp_app: :my_app_name,
  i18n_backend: MyApp.CustomMessagesBackend

Development

Getting Started

Torch currently uses Node 14 to build its assets.

Building the Torch asset bundles

The JavaScript bundle is output to priv/static/torch.js, and the CSS bundles are output to priv/static/base.css and priv/static/theme.css.

To build the bundles navigate to the assets folder and run the following commands:

$ cd assets
$ npm i
$ npm run compile

More Repositories

1

jeet

The most advanced, yet intuitive, grid system available for Sass or Stylus
CSS
2,985
star
2

scrivener_ecto

Paginate your Ecto queries with Scrivener
Elixir
572
star
3

scrivener

Pagination for the Elixir ecosystem
Elixir
553
star
4

pioneer

Integration Testing
CoffeeScript
527
star
5

stickymojo

A contained fixed positioned sticky sidebar jQuery plugin.
JavaScript
401
star
6

json-type-validation

TypeScript JSON type validation
TypeScript
156
star
7

sass2stylus

Kewl
JavaScript
79
star
8

social-builder

HTML
57
star
9

psd-lint

Clean up your PSDs.
CoffeeScript
38
star
10

pullr

JavaScript
33
star
11

envforcer

Enforce environment variable requirements.
Ruby
30
star
12

capybara-ui

Roles and page objects for Capybara integration testing - Formerly called Dill
Ruby
29
star
13

EPFImporter

A Python CLI tool for importing Apple Enterprise Partner Feed data, available to EPF partners, into a relational database.
Python
29
star
14

backbone.typeahead

Integrates typeahead into backbone collections. Inspired by twitter typeahead.
CoffeeScript
21
star
15

bricks

Ruby
20
star
16

mo

CSS
10
star
17

helios2

Helios 2.0
Elixir
9
star
18

situation-room

Like pingdom, but with more cachet
JavaScript
9
star
19

foodtrucks_bot

Slack bot for checking Foodtrucks in KP
Elixir
8
star
20

backbone.route-helper

Helper for your backbone routes.
CoffeeScript
8
star
21

PrismatestJS

Decouple your front-end application tests from the view implementation and make them easier to read and write.
TypeScript
8
star
22

mojo-images

Packer machine image templates for VMWare, VirtualBox, AWS, and Digital Ocean.
Shell
7
star
23

feedbag

TV first, multiuser Github events dashboard for organizations and teams.
CSS
7
star
24

pioneer-todo-mvc

Pioneer tests todo mvc
JavaScript
7
star
25

mojotech-ui

A set of styled React components for MojoTech.com user interfaces.
TypeScript
7
star
26

pivotal-swimlanes

Swimlanes for Pivotal Tracker Sprints
JavaScript
6
star
27

trani.js

Awesome Jquery Interactive Transcript Plugin for Vimeo
JavaScript
5
star
28

backbone.routerFilters

JavaScript
5
star
29

busbus

Like a bus, but more bus.
Objective-C
5
star
30

blimey_brackets

Magic Sublime Text bracket closing detector.
Python
4
star
31

entwine

Immutable dependency injection in Node.js
JavaScript
4
star
32

rubo_claus

Ruby method pattern matching inspired by functional programming
Ruby
4
star
33

angular-ui-instagram

Angular UI Instagram Directive
CoffeeScript
4
star
34

vire

Vire.js is a jQuery plugin that extends the froogaloop library for the Vimeo player. It allows you to execute code at a given point in time in a video.
JavaScript
4
star
35

pt-estimator

Pivotal Tracker pointing/estimating tool
TypeScript
4
star
36

grunt-pioneer

Grunt runner for pioneer.
CoffeeScript
4
star
37

modernator-haskell

An API server for hosting Reddit AMA style Q&A sessions.
Haskell
4
star
38

semaphorestatus

semaphorestatus
JavaScript
3
star
39

modernator

Moderate.
Clojure
3
star
40

modernator-client-react

A small client for mojotech/modernator-haskell
JavaScript
3
star
41

local

Configure your local dev environment. Boring.
Shell
3
star
42

docker-images

Images of Dockers.
Shell
3
star
43

marionette-ui

UI Toolkit built on Marionette
CSS
3
star
44

pioneer.marionette

Make testing marionette apps with pioneer a pleasant experience
CoffeeScript
3
star
45

modash

Useful lodash mixins
CoffeeScript
3
star
46

habit-api

Habit App
Ruby
3
star
47

lunchbot

Elixir
2
star
48

guestbook

Guestbook app
JavaScript
2
star
49

stylus-redesign

The workgroup for Stylus' homepage redesign
2
star
50

heroku-deploy

Ruby
2
star
51

nearest

A node utility for finding the nearest x minutes for a given time
CoffeeScript
2
star
52

covenant

Ruby
2
star
53

random-run-flutter

Random Run revived with Flutter
Dart
2
star
54

mofun

Like regular fun, but mo'.
JavaScript
1
star
55

helios2-elixir

Helios 2 but... in Elixir
JavaScript
1
star
56

rails-json-errors

Easily parse and handle json error packets from a Rails API
CoffeeScript
1
star
57

rubocop-privacy

Vertical alignment code style checking for ruby files
Ruby
1
star
58

standauf

HTML
1
star
59

docbert

Literate features.
Ruby
1
star
60

haml-coffee-brunch

Brunch plugin to compile haml-coffee templates
JavaScript
1
star
61

mocomments

More comments. Please.
CoffeeScript
1
star
62

pivotal-watch

Npm command line tool for watching a pivotal tracker project
JavaScript
1
star
63

node-selenium-webdriver

Import of the selenium-webdriver for node.js.
JavaScript
1
star
64

mojo-ghost-s3-adapter

Ghost blog AWS S3 storage adapter
JavaScript
1
star
65

rails_db_protect

Protect your production database from destructive rake tasks
Ruby
1
star
66

pioneer-www

JavaScript
1
star
67

batchelor

Override find_in_batches and find_each in ActiveRecord decendant classes that do not have an integer primary key as these will not work when using PostgreSQL.
1
star
68

mojotech-design

MojoTech, but make it design
TypeScript
1
star
69

mojotech-intern-docs

MojoTech Internship Documentation
JavaScript
1
star
70

next-props-wiping-bug-demo

Demonstrating a bug with NextJS wiping props
TypeScript
1
star
71

hive

A documentation-driven development revolution
Ruby
1
star