active_crud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9c7383306d45a72f92d30954133357283f34123c731913f8329c92255eea8cd2
4
+ data.tar.gz: d67f6e61e4326ac3ecf58efdae75f25b1342a5f1fc3a60f50ffe1d85a91e42b3
5
+ SHA512:
6
+ metadata.gz: abe464f5bfadceaecedd58c0eb72245abe796e5578ef91c4c8e8ffd16a502b603f8118c4a9d8e2692701bf986cf42030038d56ed1da04021cccde43bb9742ab6
7
+ data.tar.gz: fc2ba0977f7d3405350be57b148008a1baf9cc2aebb39f55f350d4a0e80308f48160b323e2cd399fb41036abae1172e469cd3d0fe9cf88be2986529094cd8e5f
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-26
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nishi.sharma01@nagarro.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in active_crud.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 nishikant-s
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,220 @@
1
+ The ActiveCrud gem is a utility library for performing common data operations in Rails applications. It provides convenient methods for creating records, retrieving records, updating records, deleting records, sorting records, paginating records, and searching records.
2
+
3
+ ## Features
4
+
5
+ - CRUD operations: Create, retrieve, update, and delete records.
6
+ - Sorting: Sort records based on column names and directions.
7
+ - Pagination: Display a limited number of records per page.
8
+ - Searching: Search records based on specific parameters.
9
+
10
+ ## Installation
11
+
12
+ Add the ActiveCrud gem to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'active_crud'
16
+ ```
17
+ ### Dependencies
18
+ For Pagination Use Gem 'will_paginate'
19
+ ```ruby
20
+ gem 'will_paginate'
21
+ ```
22
+
23
+ Then, run the following command to install the gem:
24
+
25
+ ```ruby
26
+ bundle install
27
+ ```
28
+ # Features
29
+ ### Creating a Record
30
+ To create a record, use the ActiveCrud.create_record method:
31
+ ```ruby
32
+ record_params = { attribute1: value1, attribute2: value2, ... }
33
+ result = ActiveCrud.create_record(ModelClass, record_params)
34
+ ```
35
+ The create_record method returns a result hash containing the following keys:
36
+ :message if the record is created successfully, along with the created record.
37
+ :error if the record fails to create, along with the validation errors.
38
+
39
+ ### Retrieving All Records
40
+ To retrieve all records of a model, use the ActiveCrud.retrieve_all_records method:
41
+ ```ruby
42
+ records = ActiveCrud.retrieve_all_records(ModelClass)
43
+ ```
44
+ This method returns a collection of all records for the specified model.
45
+
46
+ ### Retrieving a Specific Record
47
+ To retrieve a specific record by its ID, use the ActiveCrud.retrieve_record method:
48
+ ```ruby
49
+ result = ActiveCrud.retrieve_record(ModelClass, id)
50
+ ```
51
+ The retrieve_record method returns the found record if it exists, or an error message if the record was not found.
52
+
53
+ ### Updating a Record
54
+ To update a record with new parameters, use the ActiveCrud.update_record method:
55
+ ```ruby
56
+ record_params = { attribute1: new_value1, attribute2: new_value2, ... }
57
+ result = ActiveCrud.update_record(ModelClass, id, record_params)
58
+ ```
59
+ The update_record method returns a result hash containing the following keys:
60
+ :message if the record is updated successfully, along with the updated record.
61
+ :error if the record fails to update, along with the validation errors.
62
+ ### Deleting a Record
63
+ To delete a record by its ID, use the ActiveCrud.delete_record method:
64
+ ```ruby
65
+ result = ActiveCrud.delete_record(ModelClass, id)
66
+ ```
67
+ The delete_record method returns a result hash containing the following keys:
68
+ :message if the record is deleted successfully.
69
+ :error if the record fails to delete.
70
+ ### Sorting Records
71
+ To sort records based on a column name and direction, use the ActiveCrud.sort_records method:
72
+ ```ruby
73
+ sorted_records = ActiveCrud.sort_records(ModelClass, column, direction)
74
+ ```
75
+ The sort_records method returns a collection of records sorted based on the specified column name and direction.
76
+
77
+ ### Paginating Records
78
+ To paginate records and display a limited number of results per page, use the ActiveCrud.paginate_records method:
79
+ ```ruby
80
+ paginated_records = ActiveCrud.paginate_records(ModelClass, page, per_page)
81
+ ```
82
+ The paginate_records method returns a collection of records for the specified page number and number of records per page.
83
+
84
+ ### Searching Records
85
+ To search records based on specific parameters, use the ActiveCrud.search_records method:
86
+ ```ruby
87
+ search_params = { attribute1: value1, attribute2: value2, ... }
88
+ searched_records
89
+ ```
90
+
91
+ # Working Example with real Rails Application
92
+ #### Gemfile
93
+ gem 'active_crud'
94
+
95
+ #### app/models/user.rb
96
+ ```ruby
97
+ class User < ApplicationRecord
98
+ validates :first_name, :last_name, :email, :password, presence: true
99
+ validates :email, uniqueness: true
100
+ end
101
+ ```
102
+ #### app/controllers/users_controller.rb
103
+ ```ruby
104
+ class UsersController < ApplicationController
105
+ def index
106
+ @users = ActiveCrud.retrieve_all_records(User)
107
+ if params[:search].present?
108
+ @users = ActiveCrud.search_records(@users, %w[first_name last_name],
109
+ sanitize_params(params[:search]))
110
+ end
111
+ if params[:sort].present?
112
+ @users = ActiveCrud.sort_records(@users, sanitize_params(params[:sort]),
113
+ sanitize_params(params[:direction]))
114
+ end
115
+ @users = ActiveCrud.paginate_records(@users, sanitize_params(params[:page]), 10)
116
+ end
117
+
118
+ def new
119
+ @user = User.new
120
+ end
121
+
122
+ def create
123
+ result = ActiveCrud.create_record(User, user_params)
124
+
125
+ if result[:record]
126
+ redirect_to result[:record], notice: result[:message]
127
+ else
128
+ @user = User.new(user_params)
129
+ flash.now[:error] = result[:error]
130
+ render :new
131
+ end
132
+ end
133
+
134
+ def show
135
+ @user = ActiveCrud.retrieve_record(User, params[:id])
136
+ redirect_to users_path, alert: @user[:error] if @user[:error]
137
+ end
138
+
139
+ def edit
140
+ @user = ActiveCrud.retrieve_record(User, params[:id])
141
+ redirect_to users_path, alert: @user[:error] if @user[:error]
142
+ end
143
+
144
+ def update
145
+ result = ActiveCrud.update_record(User, params[:id], user_params)
146
+
147
+ if result[:record]
148
+ redirect_to result[:record], notice: result[:message]
149
+ else
150
+ @user = ActiveCrud.retrieve_record(User, params[:id])
151
+ flash.now[:error] = result[:error]
152
+ render :edit
153
+ end
154
+ end
155
+
156
+ def destroy
157
+ result = ActiveCrud.delete_record(User, params[:id])
158
+
159
+ redirect_to users_path, notice: result[:message] if result[:message]
160
+ redirect_to users_path, alert: result[:error] if result[:error]
161
+ end
162
+
163
+ private
164
+
165
+ def sanitize_params(value)
166
+ value.present? ? Arel.sql(value) : nil
167
+ end
168
+
169
+ def user_params
170
+ params.require(:user).permit(:first_name, :last_name, :email, :password)
171
+ end
172
+ end
173
+ ```
174
+
175
+ #### app/views/users/index.html.erb
176
+ ```ruby
177
+ <h1>Users</h1>
178
+ <%= form_tag users_path, method: :get do %>
179
+ <%= text_field_tag :search, params[:search] %>
180
+ <%= submit_tag "Search" %>
181
+ <% end %>
182
+
183
+ <table>
184
+ <thead>
185
+ <tr>
186
+ <%= link_to 'First Name', { sort: 'first_name', direction: sort_direction('first_name') } %>
187
+ <%= link_to 'Last Name', { sort: 'last_name', direction: sort_direction('last_name') } %>
188
+ <%= link_to 'Email', { sort: 'email', direction: sort_direction('email') } %>
189
+ <th>Actions</th>
190
+ </tr>
191
+ </thead>
192
+
193
+ <tbody>
194
+ <% @users.each do |user| %>
195
+ <tr>
196
+ <td><%= user.first_name %></td>
197
+ <td><%= user.last_name %></td>
198
+ <td><%= user.email %></td>
199
+ <td>
200
+ <%= link_to 'Show', user_path(user) %>
201
+ <%= link_to 'Edit', edit_user_path(user) %>
202
+ <%= link_to "Delete", user_path(user), method: :delete, data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
203
+
204
+ </td>
205
+ </tr>
206
+ <% end %>
207
+ </tbody>
208
+ </table>
209
+ <%= will_paginate %>
210
+
211
+ <p>
212
+ <%= link_to 'New User', new_user_path %>
213
+ </p>
214
+ ```
215
+ #### app/helpers/application_helper.rb
216
+ ```ruby
217
+ def sort_direction(column)
218
+ column == params[:sort] && params[:direction] == 'asc' ? 'desc' : 'asc'
219
+ end
220
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveCrud
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'active_crud/version'
4
+ require 'will_paginate'
5
+
6
+ module ActiveCrud
7
+ class Error < StandardError; end
8
+
9
+ def self.create_record(model, record_params)
10
+ record = model.new(record_params)
11
+
12
+ if record.save
13
+ { message: 'Record created successfully', record: record }
14
+ else
15
+ { error: 'Failed to create record', errors: record.errors }
16
+ end
17
+ end
18
+
19
+ def self.retrieve_all_records(model)
20
+ model.all
21
+ end
22
+
23
+ def self.retrieve_record(model, id)
24
+ model.find(id)
25
+ rescue ActiveRecord::RecordNotFound
26
+ { error: 'Record not found' }
27
+ end
28
+
29
+ def self.update_record(model, id, params)
30
+ record = model.find(id)
31
+
32
+ if record.update(params)
33
+ { message: 'Record updated successfully', record: record }
34
+ else
35
+ { error: 'Failed to update record', errors: record.errors }
36
+ end
37
+ rescue ActiveRecord::RecordNotFound
38
+ { error: 'Record not found' }
39
+ end
40
+
41
+ def self.delete_record(model, id)
42
+ record = model.find(id)
43
+
44
+ if record.destroy
45
+ { message: 'Record deleted successfully' }
46
+ else
47
+ { error: 'Failed to delete record' }
48
+ end
49
+ rescue ActiveRecord::RecordNotFound
50
+ { error: 'Record not found' }
51
+ end
52
+
53
+ def self.sort_records(model, attribute, order = 'asc')
54
+ model.order(attribute => order)
55
+ end
56
+
57
+ def self.paginate_records(model, page, per_page)
58
+ page = page.to_i.positive? ? page.to_i : 1
59
+ model.paginate(page: page, per_page: per_page)
60
+ end
61
+
62
+ def self.search_records(model, attribute_names, search, model_name = nil)
63
+ model_name ||= model.name
64
+ conditions = []
65
+
66
+ attribute_names.each do |attribute_name|
67
+ conditions << "#{model_name.downcase.pluralize}.#{attribute_name} LIKE '%#{search}%'" if search.present?
68
+ end
69
+
70
+ if conditions.present?
71
+ model.where(conditions.join(' OR '))
72
+ else
73
+ model.all
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveCrud
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_crud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nishienquiry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
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: will_paginate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ransack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: https://github.com/nishienquiry/active_crud
56
+ email:
57
+ - nishi.enquiry@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/active_crud.rb
69
+ - lib/active_crud/version.rb
70
+ - sig/active_crud.rbs
71
+ homepage: https://rubygems.org/gems/active_crud
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org
76
+ homepage_uri: https://rubygems.org/gems/active_crud
77
+ source_code_uri: https://github.com/nishienquiry/active_crud
78
+ changelog_uri: https://github.com/nishienquiry/active_crud
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.5.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.2.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: https://github.com/nishienquiry/active_crud
98
+ test_files: []