irie 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70d0c4b896c361b3118094166c14bae9a734a530
4
- data.tar.gz: ea3ff0813dabc51ba793191d92797f8c9f2b6704
3
+ metadata.gz: 83d55681746b7b5b2d5ecc207a41000e69bf299c
4
+ data.tar.gz: 3bba10948d23a3c1676b0c4513cbcb6e37e01314
5
5
  SHA512:
6
- metadata.gz: 9733f3a4277253428161f1c4b735832fbd5f53bf958b48236a90ec2a5249d143fe476d15814536126d3a690e47ef9fc5c53e6271af89dca314069837072acb62
7
- data.tar.gz: c4b1a88c2571906e7378ee09a76409f2669dcd41e5402da6b55927881dfd33a9baf4e9bb248e6cbedd9dea7de921e05cceac16a6e0f56b17e8ac0ce9e4cdf7ba
6
+ metadata.gz: 9d25b2cf8efe484b2e074bcf8832b3cfb508b24bf3d3106ffba5bba1a0dbe88695e02a92a142962ce3cb056ca63c134a3e5c6af915efa1edd14e86f7e6da2f2f
7
+ data.tar.gz: c5e2b0844fb810cbbf51beb00d68ebaca098f6612a96bd20054731674c9c9f5a0ef97206e41a8fc493724a34c06adc7ba667671b168d921262be5b01dcb93e11
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Inherited Resources including extensions. Tested with Rails 4/edge, Inherited Resources 1.4/edge in Ruby 1.9.3, 2.0.0, and jruby-19mode.
6
6
 
7
- Extend [Inherited Resources][inherited_resources] actions with the `extensions` method which provides symbolic references to do module includes as well as automatic inclusion of modules based on what actions are in-use. The included extensions provide more of a DSL-like way to define your controllers, and instead of model-heavy development via `scope` in models and `has_scope` in the controller, you can just define request parameter-based filters and their defaults in the controller. Also, ordering, parameter conversion, param value split delimiters, pagination, and more are supported.
7
+ Extend [Inherited Resources][inherited_resources] actions with the `extensions` method which provides symbolic references to do module includes as well as automatic inclusion of modules based on what `actions` are defined. The included extensions provide more of a DSL-like way to define your controllers. And, instead of model-heavy development via `scope` in models and `has_scope` in the controller, you can just define request parameter-based filters and their defaults in the controller. Ordering, parameter value conversion, pagination, and more are supported without additional dependencies.
8
8
 
9
9
  ```ruby
10
10
  class PostsController < ApplicationController
@@ -246,45 +246,7 @@ The return value of the lambda becomes the new query, so you could really change
246
246
 
247
247
  ##### Customizing Request Parameter Value Conversion
248
248
 
249
- Implement the `convert_param(param_name, param_values)` in your controller or an included module, e.g.
250
-
251
- ```ruby
252
- # example of custom parameter value converter
253
- module Example
254
- module BooleanParams
255
- extend ::ActiveSupport::Concern
256
-
257
- TRUE_VALUE = 'true'.freeze
258
- FALSE_VALUE = 'false'.freeze
259
-
260
- protected
261
-
262
- # Converts request param value(s) 'true' to true and 'false' to false
263
- def convert_param(param_name, param_value_or_values)
264
- logger.debug("Example::BooleanParams.convert_param(#{param_name.inspect}, #{param_value_or_values.inspect})") if ::Irie.debug?
265
- param_value_or_values = super if defined?(super)
266
- if param_value_or_values.is_a? Array
267
- param_value_or_values.map {|v| convert_boolean(v)}
268
- else
269
- convert_boolean(param_value_or_values)
270
- end
271
- end
272
-
273
- private
274
-
275
- def convert_boolean(value)
276
- case value
277
- when TRUE_VALUE
278
- true
279
- when FALSE_VALUE
280
- false
281
- else
282
- value
283
- end
284
- end
285
- end
286
- end
287
- ```
249
+ Implement the `convert_param(param_name, param_values)` in your controller or an included module. See [Writing Your Own Extensions](#writing-your-own-extensions) for an example.
288
250
 
289
251
  #### Default Filters
290
252
 
@@ -488,79 +450,78 @@ The following concerns, which you can include via `extensions ...` or via includ
488
450
 
489
451
  Extensions are just modules. There is no magic.
490
452
 
491
- The somewhat special thing about Irie extensions if that you can `@action_result = ...; throw(:action_break)` in any method that is called by an Irie action and it will break the execution of the action and return `@action_result`. This allows the ease of control that you'd have typically in a single long action method, but lets you use modules to easily share action method functionality. To those unfamiliar, `throw` in Ruby is a normal flow control mechanism, unlike `raise` which is for exceptions.
453
+ Some hopefully good examples of how to extend modules are in this project in `lib/irie/extensions/`.
492
454
 
493
- Some hopefully good examples of how to extend modules are in lib/irie/extensions/* and the actions themselves are in lib/irie/actions/*. Get familiar with the code even if you don't plan on customizing, if for no other reason than to have another set of eyes on the code.
494
-
495
- Here's another example:
455
+ Here's a quick example:
496
456
 
497
457
  ```ruby
498
- # Converts all 'true' and 'false' param values to true and false
499
- module BooleanParams
500
- extend ::ActiveSupport::Concern
501
-
502
- protected
503
-
504
- def convert_param(param_name, param_values)
505
- case param_values
506
- when 'true'
507
- true
508
- when 'false'
509
- false
510
- else
511
- super if defined?(super)
512
- end
513
- end
514
-
515
- end
516
- ```
517
-
518
- If you are just doing regular `include`'s in your controllers, that's all you need. If you'd like to use `extensions`, you get autoincludes and can use symbols, e.g. in `app/controllers/concerns/service_controller.rb`:
458
+ module Example
459
+ module BooleanParams
460
+ extend ::ActiveSupport::Concern
519
461
 
520
- ```ruby
521
- module ServiceController
522
- extend ::ActiveSupport::Concern
462
+ TRUE_VALUE = 'true'.freeze
463
+ FALSE_VALUE = 'false'.freeze
523
464
 
524
- included do
525
- inherit_resources
526
- respond_to :json
465
+ protected
527
466
 
528
- # reference as string so we don't load the concern before it is used.
529
- ::Irie.register_extension :boolean_params, '::Example::BooleanParams'
467
+ # Converts request param value(s) 'true' to true and 'false' to false
468
+ def convert_param(param_name, param_value_or_values)
469
+ logger.debug("Example::BooleanParams.convert_param(#{param_name.inspect}, #{param_value_or_values.inspect})") if ::Irie.debug?
470
+ param_value_or_values = super if defined?(super)
471
+ if param_value_or_values.is_a? Array
472
+ param_value_or_values.map {|v| convert_boolean(v)}
473
+ else
474
+ convert_boolean(param_value_or_values)
475
+ end
476
+ end
530
477
 
531
- # register_extension also can define the order of inclusion, e.g.:
532
- # ::Irie.register_extension :boolean_params, '::Example::BooleanParams', include: :last #default
533
- # ::Irie.register_extension :boolean_params, '::Example::BooleanParams', include: :first
534
- # ::Irie.register_extension :boolean_params, '::Example::BooleanParams', after: :nil_params
535
- # ::Irie.register_extension :boolean_params, '::Example::BooleanParams', before: :nil_params
478
+ private
536
479
 
480
+ def convert_boolean(value)
481
+ case value
482
+ when TRUE_VALUE
483
+ true
484
+ when FALSE_VALUE
485
+ false
486
+ else
487
+ value
488
+ end
489
+ end
537
490
  end
538
-
539
491
  end
540
492
  ```
541
493
 
542
- Now you could use this in your controller:
494
+ If you are just doing regular `include`'s in your controllers, that's all you need, and you can include when you need to.
543
495
 
544
- ```ruby
545
- include ServiceController
496
+ If you'd like to use your modules via the `extensions` method, just register the extension in an initializer, e.g. in `config/initializers/irie.rb`:
546
497
 
547
- actions :index
548
- extensions :boolean_params
498
+ ```ruby
499
+ # note: Referencing as string so we don't load the concern before it is used.
500
+ ::Irie.register_extension :boolean_params, '::Example::BooleanParams'
549
501
  ```
550
502
 
551
- Doing that doesn't make as much sense when you just have modules in the root namespace, but it might if you have longer namespaces for organization and to avoid class/module name conflicts.
503
+ Now, you could do this in your controller:
552
504
 
553
- #### Primary Keys
505
+ ```ruby
506
+ respond_to :json
507
+ inherit_resources
554
508
 
555
- Supports composite primary keys. If `resource_class.primary_key.is_a?(Array)`, show/edit/update/destroy will use your two or more request params for the ids that make up the composite.
509
+ actions :index
510
+ extensions :boolean_params
511
+ ```
556
512
 
557
- #### Exception Handling
513
+ Irie includes a way to specify order of module inclusion independent of the class/module it is included in, and you can specify that at registration, e.g. in an initializer like `config/initializers/irie.rb`, you might do one of the following:
558
514
 
559
- Rails 4 has basic exception handling in the [public_exceptions][public_exceptions] and [show_exceptions][show_exceptions] Rack middleware.
515
+ ```ruby
516
+ ::Irie.register_extension :boolean_params, '::Example::BooleanParams', include: :last # last is the default, so don't need to specify this option
517
+ ::Irie.register_extension :boolean_params, '::Example::BooleanParams', include: :first
518
+ ::Irie.register_extension :boolean_params, '::Example::BooleanParams', after: :nil_params
519
+ ::Irie.register_extension :boolean_params, '::Example::BooleanParams', before: :nil_params
520
+ ```
560
521
 
561
- If you want to customize Rails 4's Rack exception handling, search the web for customizing `config.exceptions_app`, although the default behavior should work for most.
522
+ Note: an extension must be registered before you can use `after:` or `before:` to place your extension include after or before it. Use `require` and declare dependencies if possible to ensure registration of other extensions, and nothing is stopping you from registering something else. The extension class constant isn't going to be referenced by Irie until `extensions` is called with it.
562
523
 
563
- You can also use `rescue_from` or `around_action` in Rails to have more control over error rendering.
524
+ The `extensions` method is just a companion to `actions` if you want to use it. You can still use [include/extend/prepend](http://ruby-doc.org/core-2.0.0/Module.html), if you'd rather.
564
525
 
565
526
  ### Troubleshooting
566
527
 
@@ -73,6 +73,11 @@ module Irie
73
73
  end
74
74
 
75
75
  def resource
76
+ cached = get_resource_ivar
77
+ if cached
78
+ logger.debug("Irie::Extensions::QueryIncludes.resource returning cached resource") if ::Irie.debug?
79
+ return cached
80
+ end
76
81
  logger.debug("Irie::Extensions::QueryIncludes.resource") if ::Irie.debug?
77
82
  this_includes = self.action_to_query_includes[params[:action].to_sym] || self.all_action_query_includes
78
83
  if this_includes && this_includes.size > 0
@@ -88,6 +93,11 @@ module Irie
88
93
  end
89
94
 
90
95
  def build_resource
96
+ cached = get_resource_ivar
97
+ if cached
98
+ logger.debug("Irie::Extensions::QueryIncludes.resource returning cached resource") if ::Irie.debug?
99
+ return cached
100
+ end
91
101
  logger.debug("Irie::Extensions::QueryIncludes.build_resource") if ::Irie.debug?
92
102
  this_includes = self.action_to_query_includes[params[:action].to_sym] || self.all_action_query_includes
93
103
  if this_includes && this_includes.size > 0
data/lib/irie/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Irie
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: irie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary S. Weaver
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-11 00:00:00.000000000 Z
12
+ date: 2013-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: inherited_resources