Giter VIP home page Giter VIP logo

gemgem-trbrb's Introduction

Gemgem

This is the sample application for the Trailblazer book.

The gemgem app is an Open Source repository. Nevertheless, I kindly ask you to buy my book even if you can learn everything you need from this repository.

Creating Trailblazer and working on all the involved gems over the past 10 years has consumed an incredible amount of my time. I loved and still love every minute of it but a way to support that is purchasing a copy of the book.

Thanks so much!

Branches

For each chapter there is a corresponding branch.

N Chapter Branch Compare
03 Operations and Forms chapter-03
04 Cells chapter-04 chapter-03...chapter-04
05 Nested Forms chapter-05 chapter-04...chapter-05
06 Composed Views chapter-06 chapter-05...chapter-06
07 Mastering Forms chapter-07 chapter-06...chapter-07
08 Callbacks chapter-08 chapter-07...chapter-08
09 Authentication chapter-09 chapter-08...chapter-09
10 Authorization and Polymorphism chapter-10 chapter-09...chapter-10
11 Hypermedia APIs: Rendering chapter-11 chapter-10...chapter-11
11 Hypermedia APIs: Deserialization chapter-11 chapter-10...chapter-11

If you find bugs, please try to fix them in the corresponding chapter branch, I'll do the rest! :)

gemgem-trbrb's People

Contributors

abinoam avatar apotonick avatar davidgrieser avatar jamie avatar rogerleite avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gemgem-trbrb's Issues

What does this bit of Ruby wizardry do?

This is in regards to an example in the book for pre-populating a nested form:

property :user, prepopulate: ->(*) { User.new } do
  property :email
end

I can not figure out, for the life of me, what this is:

->(*) { ... }

What does that stuff do? Why would you not be able to just pass User.new to the prepopulate message?

Classes cacheing issue?

I'm seeing an odd issue in my tests and when running the site - the classes variable seems to be cached somewhere such that on each subsequent call/refresh the end class gets added on again.

Do you have any idea why that might be happening?

screen shot 2015-05-29 at 10 07 30

Uninitialized constant

On the branch master, after running bundle

Andrew-Grimms-MacBook-Pro:gemgem-trbrb agrimm$ bin/rails s
=> Booting Thin
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
["app/concepts/api/v1.rb",
 "app/concepts/user/operation.rb",
 "app/concepts/thing/callback.rb",
 "app/concepts/thing/cell.rb",
 "app/concepts/thing/contract.rb",
 "app/concepts/thing/cell/form.rb",
 "app/concepts/thing/policy.rb",
 "app/concepts/thing/delete.rb",
 "app/concepts/thing/signed_in.rb",
 "app/concepts/thing/operation.rb",
 "app/concepts/session/impersonate.rb",
 "app/concepts/session/operation.rb",
 "app/concepts/navigation/cell.rb",
 "app/concepts/comment/cell.rb",
 "app/concepts/comment/operation.rb",
 "app/concepts/api/v1/thing/representer.rb",
 "app/concepts/api/v1/comment/representer.rb",
 "app/concepts/api/v1/comment/operation.rb",
 "app/concepts/api/v1/thing/operation.rb",
 "app/concepts/api/v1/comment/representer.rb",
 "app/concepts/api/v1/comment/operation.rb",
 "app/concepts/api/v1/thing/representer.rb",
 "app/concepts/api/v1/thing/operation.rb"]
Exiting
/Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:21:in `block in <class:Index>': uninitialized constant Comment::Representer (NameError)
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:20:in `initialize'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:20:in `new'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:20:in `<class:Index>'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:17:in `<module:Representer>'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:3:in `<module:Thing>'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:2:in `<module:V1>'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:1:in `<top (required)>'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-rails-0.2.3/lib/trailblazer/rails/railtie.rb:7:in `block in load_concepts'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.5/lib/trailblazer/loader.rb:65:in `block in load_files'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.5/lib/trailblazer/loader.rb:65:in `each'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.5/lib/trailblazer/loader.rb:65:in `load_files'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.5/lib/trailblazer/loader.rb:31:in `call'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-rails-0.2.3/lib/trailblazer/rails/railtie.rb:7:in `load_concepts'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/trailblazer-rails-0.2.3/lib/trailblazer/rails/railtie.rb:20:in `block (2 levels) in <class:Railtie>'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:446:in `instance_exec'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:446:in `block in make_lambda'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:192:in `call'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:192:in `block in simple'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `call'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `block in call'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `each'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:504:in `call'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:778:in `_run_prepare_callbacks'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.5/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/reloader.rb:55:in `prepare!'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/application/finisher.rb:50:in `block in <module:Finisher>'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/config/environment.rb:5:in `<top (required)>'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/config.ru:3:in `block in <main>'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/config.ru:in `new'
    from /Users/agrimm/ruby/wip/gemgem-trbrb/config.ru:in `<main>'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/server.rb:61:in `app'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/server.rb:78:in `start'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/agrimm/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'
Andrew-Grimms-MacBook-Pro:gemgem-trbrb agrimm$ 

In the weeds on uploads

Hey guys. I like to use the carrierwave_direct gem to do direct uploads to S3. In this new project, I'm creating operations for an Upload model I have.

carrierwave_direct extends AR validations to offer an :is_uploaded feature.

Like all validations, I want to put that in the contract in Create operation in app/concepts/upload/crud.rb.

To make a field on a model "uploadable" with this gem, you have to add:

mount_uploader :<fieldname>, <YourUploaderClass>

to your model. I'm trying to put it in my Upload namespace in the crud.rb file, which is where it really belongs.

I'm getting an error on the validation though, and it's a screwed-up error:

/Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:479:in `load_missing_constant': A copy of #<Class:0x007fdbf6ed35f8> has been removed from the module tree but is still active! (ArgumentError)

What the bloody hell that means I don't know. Here's the code:

class Upload < ActiveRecord::Base

    mount_uploader :filename, FileUploader
    mount_uploader :log, LogUploader
    # mount_uploader :payload, PayloadUploader

    class Create < Trailblazer::Operation

      include CRUD
      model Upload, :create

      contract do
        property :filename
        property :file_type_id

        validates_presence_of :file_type_id, :filename
        validates :filename, :is_uploaded => true

      end

      def process(params)
        validate(params[:upload]) do |f|
          f.save
        end
      end

    end

    def upload_file(options = {})
      if options[:now]
        self.filename = filename.direct_fog_url(:with_path => true)
        save
      else
        FileUploader.perform_async(attributes)
      end
    end

  end

end

The error is occurring just on running a console or server. The stacktrace points to that line:

/Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:479:in `load_missing_constant': A copy of #<Class:0x007ff2f3bcc0f8> has been removed from the module tree but is still active! (ArgumentError)
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:184:in `const_missing'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activemodel-4.2.1/lib/active_model/validations/validates.rb:118:in `const_get'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activemodel-4.2.1/lib/active_model/validations/validates.rb:118:in `block in validates'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activemodel-4.2.1/lib/active_model/validations/validates.rb:113:in `each'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activemodel-4.2.1/lib/active_model/validations/validates.rb:113:in `validates'
from /Users/me/code/bumble/app/concepts/upload/crud.rb:17:in `block in <class:Create>'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/operation.rb:46:in `class_eval'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/operation.rb:46:in `contract'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/operation/crud.rb:77:in `contract'
from /Users/me/code/bumble/app/concepts/upload/crud.rb:12:in `<class:Create>'
from /Users/me/code/bumble/app/concepts/upload/crud.rb:7:in `<class:Upload>'
from /Users/me/code/bumble/app/concepts/upload/crud.rb:1:in `<top (required)>'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:457:in `load'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:457:in `block in load_file'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:647:in `new_constants_in'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:456:in `load_file'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:354:in `require_or_load'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:317:in `depend_on'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:233:in `require_dependency'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/rails/railtie.rb:9:in `block in autoload_crud_operations'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/rails/railtie.rb:4:in `glob'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/rails/railtie.rb:4:in `autoload_crud_operations'
from /Users/me/code/bumble/.gems/ruby/2.2.0/gems/trailblazer-0.2.2/lib/trailblazer/rails/railtie.rb:21:in `block (2 levels) in <class:Railtie>'

Why the (mostly) duplicate form code for Thing?

Hi @apotonick, really enjoying working with TRB :)

Is there a reason for the repetition of the Thing form in concepts/thing/views/form.haml and views/things/form.haml? Is there a better way this could be approached? Or is it the correct pattern? I'm currently trying to determine the best way to define a model's new/edit forms using Operations.

uninitialized constant Trailblazer::Test

Hey @apotonick, I started reading your book today and so far been loving it! Thanks for writing it :)

On page 59, after writing the ThingIntegrationTest, I get the following error message from guard:

gemgem_5/test/integration/test_thing.rb:3:in <top (required)>: uninitialized constant Trailblazer::Test (NameError)

and the full stacktrace is:

/Users/soroushsaffari/gitkraken_apps/gemgem_5/test/integration/test_thing.rb:3:in `<top (required)>': uninitialized constant Trailblazer::Test (NameError)
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/test_unit/test_requirer.rb:10:in `each'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/test_unit/minitest_plugin.rb:76:in `plugin_rails_init'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/minitest-5.10.1/lib/minitest.rb:80:in `block in init_plugins'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/minitest-5.10.1/lib/minitest.rb:78:in `each'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/minitest-5.10.1/lib/minitest.rb:78:in `init_plugins'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/minitest-5.10.1/lib/minitest.rb:129:in `run'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/test.rb:9:in `<top (required)>'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:138:in `require_command!'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:95:in `test'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
	from /Users/soroushsaffari/.rvm/gems/[email protected]/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
	from /Users/soroushsaffari/gitkraken_apps/gemgem_5/bin/rails:9:in `<top (required)>'
	from /Users/soroushsaffari/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /Users/soroushsaffari/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from -e:1:in `<main>'

I'm running on Rails 5.0 with Ruby 2.3.1 and here is what I have added to my Gemfile:

...
gem "trailblazer-rails"
gem "trailblazer-loader"
gem "reform", "~> 2.1.0"

group :test do
  gem "minitest-spec-rails"
  gem "minitest-rails-capybara"
  gem "minitest-line"

  gem 'guard'
  gem 'guard-minitest'
end
...

Naming instance variable on the view

How did you manage to get @thing on the show view? Shouldn't it always be @model and @form, the variables sent to the view? Is there a way to change this?

Getting uninitialized constant Comment::Representer (NameError) in master branch

@apotonick Some offtopic at the beginning. Thanks for this framework! I can't wait to use it in production at https://inlinemanual.com. Maybe tweak the Operation bit (I am now bit influenced by Hanami::Interactor), but all the concepts are really nice. I am even thinking about rewriting our serializers from AMS to Roar, but lets see how it goes :)

Now back to the business... ๐Ÿบ I am just in the middle of the book in the cells chapter and I finally got the feeling, okay lets see how it works in reality and installed this demo app. Unfortunately I got the following error:

Exiting
/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:21:in `block in <class:Index>': uninitialized constant Comment::Representer (NameError)

Full stacktrace is here:

โžœ  gemgem-trbrb git:(master) rails s
=> Booting Thin
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
digging through thing
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/thing/contract"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/thing/representer"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/thing/callback"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/thing/cell"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/thing/policy"
digging through user
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/user/contract"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/user/representer"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/user/callback"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/user/cell"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/user/policy"
digging through comment
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/comment/contract"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/comment/representer"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/comment/callback"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/comment/cell"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/comment/policy"
digging through session
@@@@@---> /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/models/session
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/session/contract"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/session/representer"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/session/callback"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/session/cell"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/session/policy"
digging through api/v1/thing
@@@@@---> /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/models/api/v1/thing
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/contract"
loading extension... "/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer"
Exiting
/home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:21:in `block in <class:Index>': uninitialized constant Comment::Representer (NameError)
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:20:in `initialize'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:20:in `new'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:20:in `<class:Index>'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:17:in `<module:Representer>'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:3:in `<module:Thing>'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:2:in `<module:V1>'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/app/concepts/api/v1/thing/representer.rb:1:in `<top (required)>'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-rails-0.2.0/lib/trailblazer/rails/railtie.rb:6:in `block in autoload_operations'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.1/lib/trailblazer/loader.rb:32:in `block (2 levels) in call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.1/lib/trailblazer/loader.rb:29:in `each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.1/lib/trailblazer/loader.rb:29:in `block in call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.1/lib/trailblazer/loader.rb:15:in `each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-loader-0.0.1/lib/trailblazer/loader.rb:15:in `call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-rails-0.2.0/lib/trailblazer/rails/railtie.rb:6:in `autoload_operations'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/trailblazer-rails-0.2.0/lib/trailblazer/rails/railtie.rb:25:in `block (2 levels) in <class:Railtie>'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:446:in `instance_exec'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:446:in `block in make_lambda'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `block in simple'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `block in call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_prepare_callbacks'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/reloader.rb:55:in `prepare!'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/application/finisher.rb:50:in `block in <module:Finisher>'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/config/environment.rb:5:in `<top (required)>'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/config.ru:3:in `require'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/config.ru:3:in `block in <main>'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/config.ru:in `new'
    from /home/wojtha/Code/ruby/trailblazer/gemgem-trbrb/config.ru:in `<main>'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/server.rb:61:in `app'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/server.rb:78:in `start'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/wojtha/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'

OOD improvement

I like this book. Thank you for writing it. I am reading through it and came across this line:

    def reset_authorships!
      model.authorships.each { |authorship| authorship.update_attribute(:confirmed, 0) }
    end

and I thought "isn't this doing too much?" It seems to be messing around with an association's AR internals, reaching 2 levels deep. Would it not be better to have a method on the authorship form object which we write, something like

def reset_and_sync!
  confirmed = 0
  sync
end

then the code in Thing could read

    def reset_authorships!
      authorships.each(&:reset_and_sync!)
    end

Just a thought. That way the thing doesn't need to know the AR internals of an associated form object.

Multiple XSS vulnerabilities

Input for a comment or user email (and possibly other data as well) with HTML in it will be rendered as HTML. I realize this is not a real application, but since people use it as a "best practices" demo, it might be wise to address this.

Some of the issues are easily fixed by just adding include Escaped to the cell code.

Arguably, though, we would want to prevent the user from getting HTML into the database via their input in the first place. This could be done by either escaping the input, stripping tags from the input, or failing with a validation error.

My somewhat selfish reason for opening this issue is that I'm currently struggling to come up with a good way of doing this that minimizes boilerplate. Failing with a validation error doesn't seem ideal (what if the user is just innocently copy-pasting HTML?), but the other options (e.g. custom type a la HtmlStripped = Dry::Types::Constructor.new(String, fn: -> v { strip_tags(v) })) don't seem right either.

I would be happy to open a PR to address this issue.

uninitialized constant Comment::Create::CRUD (NameError)

I am follow chapter 5 of the book

app/concepts/comment/operation.rb

class Comment < ActiveRecord::Base
  class Create < Trailblazer::Operation
    include CRUD
    model Comment, :create

    contract do
      property :body
      property :weight
      property :thing
      validates :body, length: { in: 6..160 }
      validates :weight, inclusion: { in: ["0", "1"] }
      validates :thing, :user, presence: true

      property :user do
        property :email
        validates :email, presence: true, email: true
      end
    end

    def process(params)
      validate(params[:comment]) do |f|
        f.save
      end
    end
    private
    def setup_model!(params)
      model.thing = Thing.find_by_id(params[:thing_id])
    end
  end
end

config/initializers/trailblazer.rb

require 'trailblazer/autoloading'

require "trailblazer/operation/dispatch"

Trailblazer::Operation.class_eval do
  include Trailblazer::Operation::Dispatch
end

require "roar/decorator"
require "roar/json/hal"

::Roar::Representer.module_eval do
  include Rails.application.routes.url_helpers
  # include Rails.app.routes.mounted_helpers

  def default_url_options
    {}
    end
end

Roar gem not found

In Gemfile the Roar gem version is defined as
gem "roar", path: "../roar" #"1.0.0"
which causes
The path/home/greg/examples/roardoes not exist.
when bundler is run.

I've tired to fix that by removing the path, so bundler could resolve dependency (to 1.0.3). However there were some errors during app startup.

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.