actionmodel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c41a9b0d2b50d220634760063ca98c2e926e7a7
4
+ data.tar.gz: 19a460bf9c6e2c17089154187607343a4ffa9bd7
5
+ SHA512:
6
+ metadata.gz: de1fed006aaf3ba649d3ba0d974176f8b79d152cd1ee7aba84e1695dc78061ef9a1188add3b68cc0df29ef505f77733680f31e48348cae576b99f86a11c4159f
7
+ data.tar.gz: 9cda7b375acc0ab89a4b69df631f06c9d438cb37b715e6471dc591ed22b6dee95e8f87f90516af023b2540d77ead5afb2c54cc234a94c86cded86fd90d8c1231
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
19
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in actionmodel.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/actionmodel/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('lib/actionmodel.rb') { 'spec' }
8
+ watch('spec/spec_helper.rb') { 'spec' }
9
+ end
10
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Max Kazarin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,147 @@
1
+ # ActionModel
2
+
3
+ [![Build Status](https://travis-ci.org/maxkazar/actionmodel.png?branch=master)](https://travis-ci.org/maxkazar/actionmodel) [![Coverage Status](https://coveralls.io/repos/maxkazar/actionmodel/badge.png?branch=master)](https://coveralls.io/r/maxkazar/actionmodel?branch=master) [![Code Climate](https://codeclimate.com/repos/52f8ba86e30ba04a62003cdb/badges/84d12fb736e9a2d3331b/gpa.png)](https://codeclimate.com/repos/52f8ba86e30ba04a62003cdb/feed)
4
+
5
+ Clean up complex model logic using action.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'actionmodel'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install actionmodel
20
+
21
+ ## Usage
22
+
23
+ If you have complex model class it can be split into clear model class and actions. For example, `Post` model
24
+
25
+ ```ruby
26
+ class Post
27
+ scope :search, ->(title) { where tile: title }
28
+ scope :page, ->(page, size) { ... }
29
+
30
+ def add_tags(tags)
31
+ ...
32
+ end
33
+
34
+ def remove_tags(tags)
35
+ ...
36
+ end
37
+ end
38
+ ```
39
+
40
+ can be split into pretty `Post` model and actions: `searchable`, `pageable`, `tagable`
41
+
42
+ ```ruby
43
+ class Post
44
+ acts_as_searchable :title, ignorecase: true
45
+ acts_as :pageable, :tagable
46
+ end
47
+ ```
48
+
49
+ Searchable action
50
+
51
+ ```ruby
52
+ module Actions
53
+ module Searchable
54
+ extend ActiveSupport::Concern
55
+
56
+ included do
57
+ scope :search, ->(text) do
58
+ actions[:searchable].fields.each do |field, options|
59
+ # field is title
60
+ # options is { ignorecase: true }
61
+ ...
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ ```
68
+
69
+ Pageable action
70
+
71
+ ```ruby
72
+ module Actions
73
+ module Pageable
74
+ extend ActiveSupport::Concern
75
+
76
+ included do
77
+ scope :page, ->(page, size) { ... }
78
+ end
79
+ end
80
+ end
81
+ ```
82
+
83
+ Tagable action
84
+
85
+ ```ruby
86
+ module Actions
87
+ module Tagable
88
+ def add_tags(tags)
89
+ ...
90
+ end
91
+
92
+ def remove_tags(tags)
93
+ ...
94
+ end
95
+ end
96
+ end
97
+ ```
98
+
99
+ Action is a ruby module and can be included into model class with DSL `acts_as` very easy
100
+
101
+ ```ruby
102
+ acts_as :action1, :action2, ...
103
+ ```
104
+
105
+ or with fields and options
106
+
107
+ ```ruby
108
+ acts_as_action1 :field1, :field2, option1: 1, options2: 2
109
+ ```
110
+
111
+ Inside action module you can get fields and options through actions method.
112
+
113
+ ```ruby
114
+ module Actions
115
+ module Action1
116
+ # class method actions
117
+
118
+ # get action fields
119
+ actions(:action1).field.keys
120
+
121
+ # iterate each field with options
122
+ actions(:action1).field.each do |field, options|
123
+ ...
124
+ end
125
+
126
+ def do_somthing
127
+ # instance method actions do the same
128
+ end
129
+ end
130
+ end
131
+ ```
132
+
133
+ ActionModel works with rails folder structure. Actions can be stored into some folders:
134
+
135
+ * local action folder path is `/app/model/post`
136
+ * global action folder path is `/app/models/actions` and `/lib/actions`
137
+
138
+ Local action has more priority then global. When actions with the same name are stored in both folder, then local action will be included.
139
+ It helps to override some action for specific model.
140
+
141
+ ## Contributing
142
+
143
+ 1. Fork it ( http://github.com/<my-github-username>/actionmodel/fork )
144
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
145
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
146
+ 4. Push to the branch (`git push origin my-new-feature`)
147
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'actionmodel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'actionmodel'
8
+ spec.version = ActionModel::VERSION
9
+ spec.authors = ['Max Kazarin']
10
+ spec.email = ['maxkazargm@gmail.com']
11
+ spec.summary = %q{Model actions extender.}
12
+ spec.description = %q{ActionModel helps to structure model logic with actions.}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'activesupport'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'guard'
27
+ spec.add_development_dependency 'guard-rspec'
28
+ spec.add_development_dependency 'coveralls'
29
+ end
@@ -0,0 +1,5 @@
1
+ require 'actionmodel/version'
2
+ require 'actionmodel/concern'
3
+
4
+ module ActionModel
5
+ end
@@ -0,0 +1,100 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+ require 'active_support/core_ext/string/inflections'
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'active_support/concern'
5
+ require 'actionmodel/context'
6
+
7
+ module ActionModel
8
+ module Concern
9
+ extend ActiveSupport::Concern
10
+
11
+ # Return actions context hash for model instance.
12
+ def actions
13
+ self.class.actions
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ # Add actions to model.
19
+ #
20
+ # Add single action to model
21
+ #
22
+ # class Post
23
+ # acts_as :searchable
24
+ # ...
25
+ # end
26
+ #
27
+ # Add actions to model
28
+ #
29
+ # class Post
30
+ # acts_as :searchable, :viewable
31
+ # ...
32
+ # end
33
+ #
34
+ # Actions can be added with options. Options applied for each action
35
+ #
36
+ # class Post
37
+ # acts_as :searchable, ignorecase: true
38
+ # ...
39
+ # end
40
+ def acts_as(*args)
41
+ options = args.extract_options!
42
+
43
+ args.each { |action_name| include_action action_name, options }
44
+ end
45
+
46
+ # Return actions context hash for model class.
47
+ def actions
48
+ @actions ||= {}
49
+ end
50
+
51
+ # Add action to model.
52
+ #
53
+ #
54
+ # @example Add historyable action
55
+ # class Rank
56
+ # acts_as_historyable :value, :boost, autofill: true
57
+ # ...
58
+ # end
59
+ def method_missing(method, *args, &block)
60
+ action = method[/^acts_as_(.+)/, 1]
61
+ if action.nil?
62
+ super method, *args, &block
63
+ else
64
+ include_action *args.unshift(action)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ # Include action into model
71
+ def include_action(*args)
72
+ return if args.empty?
73
+
74
+ action_name = args.shift.to_sym
75
+
76
+ action = action_module action_name
77
+ unless action
78
+ raise 'Action %1$s is not found in [%2$s::%3$s, Actions::%3$s].' %
79
+ [ action_name, self.name.demodulize, action_name.to_s.camelize ]
80
+ end
81
+
82
+ find_or_create_action(action_name).update *args
83
+
84
+ include action unless include? action
85
+ end
86
+
87
+ # Find or create action by action name
88
+ def find_or_create_action(action_name)
89
+ actions[action_name] ||= ActionModel::Context.new
90
+ end
91
+
92
+ # Return action module by action name
93
+ def action_module(action)
94
+ "#{self.name.demodulize}::#{action.to_s.camelize}".safe_constantize ||
95
+ "Actions::#{action.to_s.camelize}".safe_constantize
96
+ end
97
+ end
98
+ end
99
+ end
100
+
@@ -0,0 +1,24 @@
1
+ module ActionModel
2
+ class Context
3
+ def options
4
+ @options ||= {}
5
+ end
6
+
7
+ def fields
8
+ @fields ||= {}
9
+ end
10
+
11
+ def update(*args)
12
+ options = args.extract_options!
13
+
14
+ if args.empty?
15
+ self.options.merge! options
16
+ else
17
+ args.each do |field|
18
+ fields[field.to_sym] ||= {}
19
+ fields[field.to_sym].merge! options
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ActionModel
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionModel::Concern do
4
+ let(:model) { Class.new { include ActionModel::Concern } }
5
+
6
+ before do
7
+ allow(model).to receive(:name).and_return 'Post'
8
+ model.public_class_method :include_action, :find_or_create_action, :action_module
9
+ end
10
+
11
+ describe '#actions' do
12
+ it 'return class actions' do
13
+ expect(model.new.actions).to eq model.actions
14
+ end
15
+ end
16
+
17
+ describe '.acts_as' do
18
+ it 'include action' do
19
+ expect(model).to receive(:include_action).with :searchable, {}
20
+ model.acts_as :searchable
21
+ end
22
+
23
+ it 'include action with options' do
24
+ expect(model).to receive(:include_action).with :searchable, { ignorecase: true }
25
+ model.acts_as :searchable, ignorecase: true
26
+ end
27
+ end
28
+
29
+ describe '.method_missing' do
30
+ context 'without action method' do
31
+ it 'call class method' do
32
+ expect { model.some_method }.to raise_error
33
+ end
34
+ end
35
+
36
+ context 'with action method' do
37
+ it 'config action' do
38
+ expect(model).to receive(:include_action).with 'searchable', :name, ignorecase: true
39
+ model.acts_as_searchable :name, ignorecase: true
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '.include_action' do
45
+ context 'action module exists' do
46
+ let(:action) { double :action }
47
+
48
+ before do
49
+ allow(model).to receive(:action_module).and_return action
50
+ allow(model).to receive(:include?).and_return false
51
+ end
52
+
53
+ it 'update action context' do
54
+ allow(model).to receive(:include)
55
+ expect_any_instance_of(ActionModel::Context).to receive(:update)
56
+ model.include_action :searchable
57
+ end
58
+
59
+ it 'update action context with options' do
60
+ allow(model).to receive(:include)
61
+ expect_any_instance_of(ActionModel::Context).to receive(:update).with(ignorecase: true)
62
+ model.include_action :searchable, ignorecase: true
63
+ end
64
+
65
+ it 'include action module' do
66
+ expect(model).to receive(:include).with action
67
+ model.include_action :searchable
68
+ end
69
+ end
70
+
71
+ context 'action module does not exists' do
72
+ before { allow(model).to receive(:action_module).and_return nil }
73
+
74
+ it 'raise exception' do
75
+ expect { model.include_action :unknownable }.to raise_error RuntimeError, 'Action unknownable is not found in [Post::Unknownable, Actions::Unknownable].'
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '.action_module' do
81
+ let(:action) { "#{model.name.demodulize}::Knownable" }
82
+
83
+ context 'when a model action exists' do
84
+ before { allow(ActiveSupport::Inflector).to receive(:safe_constantize).with(action).and_return action }
85
+
86
+ subject { model.action_module :knownable }
87
+
88
+ it { should eq action }
89
+ end
90
+
91
+ context 'when a global action exists' do
92
+ let(:action) { 'Actions::Knownable' }
93
+
94
+ before { allow(ActiveSupport::Inflector).to receive(:safe_constantize).and_return nil, action }
95
+
96
+ subject { model.action_module :knownable }
97
+
98
+ it { should eq action }
99
+ end
100
+
101
+ context 'when the action model is not exist' do
102
+ before { allow(ActiveSupport::Inflector).to receive(:safe_constantize).and_return nil }
103
+
104
+ subject { model.action_module(:unknownable) }
105
+
106
+ it { should be_nil }
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionModel::Context do
4
+ let(:context) { ActionModel::Context.new }
5
+
6
+ describe '#update' do
7
+ it 'update action options' do
8
+ context.update disabled: true
9
+ expect(context.options).to eq disabled: true
10
+ end
11
+
12
+ it 'update action fields' do
13
+ context.update :name, :email
14
+ expect(context.fields.keys).to eq [:name, :email]
15
+ end
16
+
17
+ it 'update action fields options' do
18
+ context.update :name, :email, ignorecase: true
19
+ expect(context.fields).to eq({ name: {ignorecase: true}, email: {ignorecase: true} })
20
+ end
21
+
22
+ context 'when action context has options' do
23
+ before { context.options[:disabled] = true }
24
+
25
+ it 'update action options' do
26
+ context.update disabled: false, inverse: true
27
+ expect(context.options).to eq disabled: false, inverse: true
28
+ end
29
+ end
30
+
31
+ context 'when action context has field' do
32
+ before { context.fields[:name] = {ignorecase: true} }
33
+
34
+ it 'update action field options' do
35
+ context.update :name, :email, ignorecase: false
36
+ expect(context.fields).to eq({ name: {ignorecase: false}, email: {ignorecase: false} })
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'actionmodel'
5
+
6
+ RSpec.configure do |config|
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+
10
+ # Run specs in random order to surface order dependencies. If you find an
11
+ # order dependency and want to debug it, you can fix the order by providing
12
+ # the seed, which is printed after each run.
13
+ # --seed 1234
14
+ config.order = 'random'
15
+
16
+ config.expect_with(:rspec) { |c| c.syntax = :expect }
17
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actionmodel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max Kazarin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ActionModel helps to structure model logic with actions.
112
+ email:
113
+ - maxkazargm@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Guardfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - actionmodel.gemspec
127
+ - lib/actionmodel.rb
128
+ - lib/actionmodel/concern.rb
129
+ - lib/actionmodel/context.rb
130
+ - lib/actionmodel/version.rb
131
+ - spec/concern_spec.rb
132
+ - spec/context_spec.rb
133
+ - spec/spec_helper.rb
134
+ homepage: ''
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.2.1
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Model actions extender.
158
+ test_files:
159
+ - spec/concern_spec.rb
160
+ - spec/context_spec.rb
161
+ - spec/spec_helper.rb
162
+ has_rdoc: